1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
//------------------------------------------------------------------------------
// <copyright file="MembershipUser.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Security {
using System.Web;
using System.Configuration.Provider;
using System.Security.Principal;
using System.Security.Permissions;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Web.Util;
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
[Serializable]
[TypeForwardedFrom("System.Web, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public class MembershipUser
{
////////////////////////////////////////////////////////////
// Public methods
public virtual string UserName{
get { return _UserName;}}
public virtual object ProviderUserKey{
get { return _ProviderUserKey;}}
public virtual string Email{
get { return _Email;}
set { _Email = value; }}
public virtual string PasswordQuestion{
get { return _PasswordQuestion;}}
public virtual string Comment{
get { return _Comment;}
set { _Comment = value;}}
public virtual bool IsApproved{
get { return _IsApproved;}
set { _IsApproved = value; } }
public virtual bool IsLockedOut
{
get { return _IsLockedOut; }
}
public virtual DateTime LastLockoutDate
{
get { return _LastLockoutDate.ToLocalTime(); }
}
public virtual DateTime CreationDate {
get { return _CreationDate.ToLocalTime(); }
}
public virtual DateTime LastLoginDate {
get { return _LastLoginDate.ToLocalTime(); }
set { _LastLoginDate = value.ToUniversalTime(); } }
public virtual DateTime LastActivityDate {
get { return _LastActivityDate.ToLocalTime(); }
set { _LastActivityDate = value.ToUniversalTime(); } }
public virtual DateTime LastPasswordChangedDate {
get { return _LastPasswordChangedDate.ToLocalTime(); }
}
public virtual bool IsOnline {
get {
TimeSpan ts = new TimeSpan(0, SystemWebProxy.Membership.UserIsOnlineTimeWindow, 0);
DateTime dt = DateTime.UtcNow.Subtract(ts);
return LastActivityDate.ToUniversalTime() > dt;
}
}
public override string ToString()
{
return UserName;
}
public virtual string ProviderName
{
get { return _ProviderName; }
}
////////////////////////////////////////////////////////////
// CTor
public MembershipUser(
string providerName,
string name,
object providerUserKey,
string email,
string passwordQuestion,
string comment,
bool isApproved,
bool isLockedOut,
DateTime creationDate,
DateTime lastLoginDate,
DateTime lastActivityDate,
DateTime lastPasswordChangedDate,
DateTime lastLockoutDate )
{
if ( providerName == null || SystemWebProxy.Membership.Providers[providerName] == null )
{
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, ApplicationServicesStrings.Membership_provider_name_invalid), "providerName" );
}
if( name != null )
{
name = name.Trim();
}
if( email != null )
{
email = email.Trim();
}
if( passwordQuestion != null )
{
passwordQuestion = passwordQuestion.Trim();
}
_ProviderName = providerName;
_UserName = name;
_ProviderUserKey = providerUserKey;
_Email = email;
_PasswordQuestion = passwordQuestion;
_Comment = comment;
_IsApproved = isApproved;
_IsLockedOut = isLockedOut;
// VSWhidbey 451539: We should use UTC internally for all dates, and return local for public apis
_CreationDate = creationDate.ToUniversalTime();
_LastLoginDate = lastLoginDate.ToUniversalTime();
_LastActivityDate = lastActivityDate.ToUniversalTime();
_LastPasswordChangedDate = lastPasswordChangedDate.ToUniversalTime();
_LastLockoutDate = lastLockoutDate.ToUniversalTime();
}
protected MembershipUser() { } // Default CTor: Callable by derived class only.
internal virtual void Update()
{
SystemWebProxy.Membership.Providers[ProviderName].UpdateUser(this);
UpdateSelf();
}
public virtual string GetPassword()
{
return SystemWebProxy.Membership.Providers[ProviderName].GetPassword(UserName, null);
}
public virtual string GetPassword(string passwordAnswer)
{
return SystemWebProxy.Membership.Providers[ProviderName].GetPassword(UserName, passwordAnswer);
}
internal string GetPassword(bool throwOnError) {
return GetPassword(null, /* useAnswer */ false, throwOnError);
}
internal string GetPassword(string answer, bool throwOnError) {
return GetPassword(answer, /* useAnswer */ true, throwOnError);
}
// GetPassword() can throw 3 types of exception:
// 1. ArgumentException is thrown if:
// A. Answer is null, empty, or longer than 128 characters
// 2. ProviderException is thrown if the user does not exist when the stored procedure
// is run. The only way this could happen is in a race condition, where the user
// is deleted in the middle of the MembershipProvider.ChangePassword() method.
// 3. MembershipPasswordException is thrown if the user is locked out, or the answer
// is incorrect.
private string GetPassword(string answer, bool useAnswer, bool throwOnError) {
string password = null;
try {
if (useAnswer) {
password = GetPassword(answer);
}
else {
password = GetPassword();
}
}
catch (ArgumentException) {
if (throwOnError) throw;
}
catch (MembershipPasswordException) {
if (throwOnError) throw;
}
catch (ProviderException) {
if (throwOnError) throw;
}
return password;
}
public virtual bool ChangePassword(string oldPassword, string newPassword)
{
SecurityServices.CheckPasswordParameter(oldPassword, "oldPassword");
SecurityServices.CheckPasswordParameter(newPassword, "newPassword");
if (!SystemWebProxy.Membership.Providers[ProviderName].ChangePassword(UserName, oldPassword, newPassword))
return false;
UpdateSelf();
//_LastPasswordChangedDate = Membership.Providers[ ProviderName ].GetUser( UserName, false ).LastPasswordChangedDate;
return true;
}
// ChangePassword() can throw 3 types of exception:
// 1. ArgumentException is thrown if:
// A. OldPassword or NewPassword is null, empty, or longer than 128 characters
// B. NewPassword shorter than MinRequiredPasswordLength, or NewPassword contains
// less non-alphanumeric characters than MinRequiredNonAlphanumericCharacters,
// or NewPassword does not match PasswordStrengthRegularExpression.
// C. A developer adds a listener to the MembershipProvider.ValidatingPassword event,
// and sets e.Cancel to true, and e.FailureInformation is null.
// 2. ProviderException is thrown if the user does not exist when the stored procedure
// is run. The only way this could happen is in a race condition, where the user
// is deleted in the middle of the MembershipProvider.ChangePassword() method.
// 3. It appears that MembershipProviderException currently cannot be thrown, but
// there is a codepath that throws this exception, so we should catch it here anyway.
internal bool ChangePassword(string oldPassword, string newPassword, bool throwOnError) {
bool passwordChanged = false;
try {
passwordChanged = ChangePassword(oldPassword, newPassword);
}
catch (ArgumentException) {
if (throwOnError) throw;
}
catch (MembershipPasswordException) {
if (throwOnError) throw;
}
catch (ProviderException) {
if (throwOnError) throw;
}
return passwordChanged;
}
public virtual bool ChangePasswordQuestionAndAnswer(string password, string newPasswordQuestion, string newPasswordAnswer)
{
SecurityServices.CheckPasswordParameter(password, "password");
SecurityServices.CheckForEmptyOrWhiteSpaceParameter(ref newPasswordQuestion, "newPasswordQuestion");
SecurityServices.CheckForEmptyOrWhiteSpaceParameter(ref newPasswordAnswer, "newPasswordAnswer");
if (!SystemWebProxy.Membership.Providers[ProviderName].ChangePasswordQuestionAndAnswer(UserName, password, newPasswordQuestion, newPasswordAnswer))
return false;
UpdateSelf();
return true;
}
public virtual string ResetPassword(string passwordAnswer)
{
string pass = SystemWebProxy.Membership.Providers[ProviderName].ResetPassword(UserName, passwordAnswer);
if (!String.IsNullOrEmpty(pass)) {
UpdateSelf();
//_LastPasswordChangedDate = Membership.Providers[ProviderName].GetUser(UserName, false).LastPasswordChangedDate;
}
return pass;
}
public virtual string ResetPassword()
{
return ResetPassword(null);
}
internal string ResetPassword(bool throwOnError) {
return ResetPassword(null, /* useAnswer */ false, throwOnError);
}
internal string ResetPassword(string passwordAnswer, bool throwOnError) {
return ResetPassword(passwordAnswer, /* useAnswer */ true, throwOnError);
}
// MembershipProvider.ResetPassword() can throw 3 types of exception:
// 1. ArgumentException is thrown if:
// A. Answer is null, empty, or longer than 128 characters
// 2. ProviderException is thrown if:
// A. The user does not exist when the stored procedure is run. The only way
// this could happen is in a race condition, where the user is deleted in
// the middle of the MembershipProvider.ChangePassword() method.
// B. A developer adds a listener to the MembershipProvider.ValidatingPassword event,
// and sets e.Cancel to true, and e.FailureInformation is null.
// 3. MembershipPasswordException is thrown if the user is locked out, or the answer
// is incorrect.
private string ResetPassword(string passwordAnswer, bool useAnswer, bool throwOnError) {
string password = null;
try {
if (useAnswer) {
password = ResetPassword(passwordAnswer);
}
else {
password = ResetPassword();
}
}
catch (ArgumentException) {
if (throwOnError) throw;
}
catch (MembershipPasswordException) {
if (throwOnError) throw;
}
catch (ProviderException) {
if (throwOnError) throw;
}
return password;
}
public virtual bool UnlockUser()
{
if (SystemWebProxy.Membership.Providers[ProviderName].UnlockUser(UserName))
{
UpdateSelf();
return !IsLockedOut;
}
return false;
}
private void UpdateSelf()
{
MembershipUser mu = SystemWebProxy.Membership.Providers[ProviderName].GetUser(UserName, false);
if (mu != null) {
try {
_LastPasswordChangedDate = mu.LastPasswordChangedDate.ToUniversalTime();
} catch (NotSupportedException) {}
try {
LastActivityDate = mu.LastActivityDate;
} catch (NotSupportedException) {}
try {
LastLoginDate = mu.LastLoginDate;
} catch (NotSupportedException) {}
try {
_CreationDate = mu.CreationDate.ToUniversalTime();
} catch (NotSupportedException) { }
try {
_LastLockoutDate = mu.LastLockoutDate.ToUniversalTime();
} catch (NotSupportedException) { }
try {
_IsLockedOut = mu.IsLockedOut;
} catch (NotSupportedException) { }
try {
IsApproved = mu.IsApproved;
} catch (NotSupportedException) { }
try {
Comment = mu.Comment;
} catch (NotSupportedException) { }
try {
_PasswordQuestion = mu.PasswordQuestion;
} catch (NotSupportedException) { }
try {
Email = mu.Email;
} catch (NotSupportedException) { }
try {
_ProviderUserKey = mu.ProviderUserKey;
} catch (NotSupportedException) { }
}
}
////////////////////////////////////////////////////////////
// private Data
private string _UserName;
private object _ProviderUserKey;
private string _Email;
private string _PasswordQuestion;
private string _Comment;
private bool _IsApproved;
private bool _IsLockedOut;
private DateTime _LastLockoutDate;
private DateTime _CreationDate;
private DateTime _LastLoginDate;
private DateTime _LastActivityDate;
private DateTime _LastPasswordChangedDate;
private string _ProviderName;
}
}
|