2 // System.Web.Security.MembershipUser
5 // Ben Maurer (bmaurer@users.sourceforge.net)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Reflection;
32 using System.Runtime.CompilerServices;
34 namespace System.Web.Security
36 [TypeForwardedFrom ("System.Web, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a")]
38 public class MembershipUser
42 object providerUserKey;
44 string passwordQuestion;
48 DateTime creationDate;
49 DateTime lastLoginDate;
50 DateTime lastActivityDate;
51 DateTime lastPasswordChangedDate;
52 DateTime lastLockoutDate;
54 protected MembershipUser ()
58 public MembershipUser (string providerName, string name, object providerUserKey, string email,
59 string passwordQuestion, string comment, bool isApproved, bool isLockedOut,
60 DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate,
61 DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
63 this.providerName = providerName;
65 this.providerUserKey = providerUserKey;
67 this.passwordQuestion = passwordQuestion;
68 this.comment = comment;
69 this.isApproved = isApproved;
70 this.isLockedOut = isLockedOut;
71 this.creationDate = creationDate.ToUniversalTime ();
72 this.lastLoginDate = lastLoginDate.ToUniversalTime ();
73 this.lastActivityDate = lastActivityDate.ToUniversalTime ();
74 this.lastPasswordChangedDate = lastPasswordChangedDate.ToUniversalTime ();
75 this.lastLockoutDate = lastLockoutDate.ToUniversalTime ();
78 void UpdateSelf (MembershipUser fromUser)
80 try { Comment = fromUser.Comment; } catch (NotSupportedException) {}
81 try { creationDate = fromUser.CreationDate; } catch (NotSupportedException) {}
82 try { Email = fromUser.Email; } catch (NotSupportedException) {}
83 try { IsApproved = fromUser.IsApproved; } catch (NotSupportedException) {}
84 try { isLockedOut = fromUser.IsLockedOut; } catch (NotSupportedException) {}
85 try { LastActivityDate = fromUser.LastActivityDate; } catch (NotSupportedException) {}
86 try { lastLockoutDate = fromUser.LastLockoutDate; } catch (NotSupportedException) {}
87 try { LastLoginDate = fromUser.LastLoginDate; } catch (NotSupportedException) {}
88 try { lastPasswordChangedDate = fromUser.LastPasswordChangedDate; } catch (NotSupportedException) {}
89 try { passwordQuestion = fromUser.PasswordQuestion; } catch (NotSupportedException) {}
90 try { providerUserKey = fromUser.ProviderUserKey; } catch (NotSupportedException) {}
93 internal void UpdateUser ()
95 MembershipUser newUser = Provider.GetUser (UserName, false);
99 public virtual bool ChangePassword (string oldPassword, string newPassword)
101 bool success = Provider.ChangePassword (UserName, oldPassword, newPassword);
108 public virtual bool ChangePasswordQuestionAndAnswer (string password, string newPasswordQuestion, string newPasswordAnswer)
110 bool success = Provider.ChangePasswordQuestionAndAnswer (UserName, password, newPasswordQuestion, newPasswordAnswer);
117 public virtual string GetPassword ()
119 return GetPassword (null);
122 public virtual string GetPassword (string answer)
124 return Provider.GetPassword (UserName, answer);
127 public virtual string ResetPassword ()
129 return ResetPassword (null);
132 public virtual string ResetPassword (string answer)
134 string newPass = Provider.ResetPassword (UserName, answer);
141 public virtual string Comment {
142 get { return comment; }
143 set { comment = value; }
146 public virtual DateTime CreationDate {
147 get { return creationDate.ToLocalTime (); }
150 public virtual string Email {
151 get { return email; }
152 set { email = value; }
155 public virtual bool IsApproved {
156 get { return isApproved; }
157 set { isApproved = value; }
160 public virtual bool IsLockedOut {
161 get { return isLockedOut; }
168 IMembershipHelper helper = MembershipProvider.Helper;
170 throw new PlatformNotSupportedException ("The method is not available.");
171 minutes = helper.UserIsOnlineTimeWindow;
172 return LastActivityDate > DateTime.Now - TimeSpan.FromMinutes (minutes);
176 public virtual DateTime LastActivityDate {
177 get { return lastActivityDate.ToLocalTime (); }
178 set { lastActivityDate = value.ToUniversalTime (); }
181 public virtual DateTime LastLoginDate {
182 get { return lastLoginDate.ToLocalTime (); }
183 set { lastLoginDate = value.ToUniversalTime (); }
186 public virtual DateTime LastPasswordChangedDate {
187 get { return lastPasswordChangedDate.ToLocalTime (); }
190 public virtual DateTime LastLockoutDate {
191 get { return lastLockoutDate.ToLocalTime (); }
194 public virtual string PasswordQuestion {
195 get { return passwordQuestion; }
198 public virtual string ProviderName {
199 get { return providerName; }
202 public virtual string UserName {
206 public virtual object ProviderUserKey {
207 get { return providerUserKey; }
210 public override string ToString ()
215 public virtual bool UnlockUser ()
217 bool retval = Provider.UnlockUser (UserName);
224 MembershipProvider Provider {
226 MembershipProvider p;
227 IMembershipHelper helper = MembershipProvider.Helper;
229 throw new PlatformNotSupportedException ("The method is not available.");
230 p = helper.Providers [ProviderName];
232 throw new InvalidOperationException ("Membership provider '" + ProviderName + "' not found.");