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
37 [TypeForwardedFrom ("System.Web, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a")]
40 public class MembershipUser
44 object providerUserKey;
46 string passwordQuestion;
50 DateTime creationDate;
51 DateTime lastLoginDate;
52 DateTime lastActivityDate;
53 DateTime lastPasswordChangedDate;
54 DateTime lastLockoutDate;
56 protected MembershipUser ()
60 public MembershipUser (string providerName, string name, object providerUserKey, string email,
61 string passwordQuestion, string comment, bool isApproved, bool isLockedOut,
62 DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate,
63 DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
65 this.providerName = providerName;
67 this.providerUserKey = providerUserKey;
69 this.passwordQuestion = passwordQuestion;
70 this.comment = comment;
71 this.isApproved = isApproved;
72 this.isLockedOut = isLockedOut;
73 this.creationDate = creationDate.ToUniversalTime ();
74 this.lastLoginDate = lastLoginDate.ToUniversalTime ();
75 this.lastActivityDate = lastActivityDate.ToUniversalTime ();
76 this.lastPasswordChangedDate = lastPasswordChangedDate.ToUniversalTime ();
77 this.lastLockoutDate = lastLockoutDate.ToUniversalTime ();
80 void UpdateSelf (MembershipUser fromUser)
82 try { Comment = fromUser.Comment; } catch (NotSupportedException) {}
83 try { creationDate = fromUser.CreationDate; } catch (NotSupportedException) {}
84 try { Email = fromUser.Email; } catch (NotSupportedException) {}
85 try { IsApproved = fromUser.IsApproved; } catch (NotSupportedException) {}
86 try { isLockedOut = fromUser.IsLockedOut; } catch (NotSupportedException) {}
87 try { LastActivityDate = fromUser.LastActivityDate; } catch (NotSupportedException) {}
88 try { lastLockoutDate = fromUser.LastLockoutDate; } catch (NotSupportedException) {}
89 try { LastLoginDate = fromUser.LastLoginDate; } catch (NotSupportedException) {}
90 try { lastPasswordChangedDate = fromUser.LastPasswordChangedDate; } catch (NotSupportedException) {}
91 try { passwordQuestion = fromUser.PasswordQuestion; } catch (NotSupportedException) {}
92 try { providerUserKey = fromUser.ProviderUserKey; } catch (NotSupportedException) {}
95 internal void UpdateUser ()
97 MembershipUser newUser = Provider.GetUser (name, false);
101 public virtual bool ChangePassword (string oldPassword, string newPassword)
103 bool success = Provider.ChangePassword (UserName, oldPassword, newPassword);
110 public virtual bool ChangePasswordQuestionAndAnswer (string password, string newPasswordQuestion, string newPasswordAnswer)
112 bool success = Provider.ChangePasswordQuestionAndAnswer (UserName, password, newPasswordQuestion, newPasswordAnswer);
119 public virtual string GetPassword ()
121 return GetPassword (null);
124 public virtual string GetPassword (string answer)
126 return Provider.GetPassword (UserName, answer);
129 public virtual string ResetPassword ()
131 return ResetPassword (null);
134 public virtual string ResetPassword (string answer)
136 string newPass = Provider.ResetPassword (UserName, answer);
143 public virtual string Comment {
144 get { return comment; }
145 set { comment = value; }
148 public virtual DateTime CreationDate {
149 get { return creationDate.ToLocalTime (); }
152 public virtual string Email {
153 get { return email; }
154 set { email = value; }
157 public virtual bool IsApproved {
158 get { return isApproved; }
159 set { isApproved = value; }
162 public virtual bool IsLockedOut {
163 get { return isLockedOut; }
175 IMembershipHelper helper = MembershipProvider.Helper;
177 throw new PlatformNotSupportedException ("The method is not available.");
178 minutes = helper.UserIsOnlineTimeWindow;
180 minutes = Membership.UserIsOnlineTimeWindow;
182 return LastActivityDate > DateTime.Now - TimeSpan.FromMinutes (minutes);
186 public virtual DateTime LastActivityDate {
187 get { return lastActivityDate.ToLocalTime (); }
188 set { lastActivityDate = value.ToUniversalTime (); }
191 public virtual DateTime LastLoginDate {
192 get { return lastLoginDate.ToLocalTime (); }
193 set { lastLoginDate = value.ToUniversalTime (); }
196 public virtual DateTime LastPasswordChangedDate {
197 get { return lastPasswordChangedDate.ToLocalTime (); }
200 public virtual DateTime LastLockoutDate {
201 get { return lastLockoutDate.ToLocalTime (); }
204 public virtual string PasswordQuestion {
205 get { return passwordQuestion; }
208 public virtual string ProviderName {
209 get { return providerName; }
212 public virtual string UserName {
216 public virtual object ProviderUserKey {
217 get { return providerUserKey; }
220 public override string ToString ()
225 public virtual bool UnlockUser ()
227 bool retval = Provider.UnlockUser (UserName);
234 MembershipProvider Provider {
236 MembershipProvider p;
238 IMembershipHelper helper = MembershipProvider.Helper;
240 throw new PlatformNotSupportedException ("The method is not available.");
241 p = helper.Providers [ProviderName];
243 p = Membership.Providers [ProviderName];
246 throw new InvalidOperationException ("Membership provider '" + ProviderName + "' not found.");