MembershipUser.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // System.Web.Security.MembershipUser
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. namespace System.Web.Security
  31. {
  32. [Serializable]
  33. public class MembershipUser
  34. {
  35. string providerName;
  36. string name;
  37. object providerUserKey;
  38. string email;
  39. string passwordQuestion;
  40. string comment;
  41. bool isApproved;
  42. bool isLockedOut;
  43. DateTime creationDate;
  44. DateTime lastLoginDate;
  45. DateTime lastActivityDate;
  46. DateTime lastPasswordChangedDate;
  47. DateTime lastLockoutDate;
  48. protected MembershipUser ()
  49. {
  50. }
  51. public MembershipUser (string providerName, string name, object providerUserKey, string email,
  52. string passwordQuestion, string comment, bool isApproved, bool isLockedOut,
  53. DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate,
  54. DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
  55. {
  56. this.providerName = providerName;
  57. this.name = name;
  58. this.providerUserKey = providerUserKey;
  59. this.email = email;
  60. this.passwordQuestion = passwordQuestion;
  61. this.comment = comment;
  62. this.isApproved = isApproved;
  63. this.isLockedOut = isLockedOut;
  64. this.creationDate = creationDate.ToUniversalTime ();
  65. this.lastLoginDate = lastLoginDate.ToUniversalTime ();
  66. this.lastActivityDate = lastActivityDate.ToUniversalTime ();
  67. this.lastPasswordChangedDate = lastPasswordChangedDate.ToUniversalTime ();
  68. this.lastLockoutDate = lastLockoutDate.ToUniversalTime ();
  69. }
  70. void UpdateSelf (MembershipUser fromUser)
  71. {
  72. try { Comment = fromUser.Comment; } catch (NotSupportedException) {}
  73. try { creationDate = fromUser.CreationDate; } catch (NotSupportedException) {}
  74. try { Email = fromUser.Email; } catch (NotSupportedException) {}
  75. try { IsApproved = fromUser.IsApproved; } catch (NotSupportedException) {}
  76. try { isLockedOut = fromUser.IsLockedOut; } catch (NotSupportedException) {}
  77. try { LastActivityDate = fromUser.LastActivityDate; } catch (NotSupportedException) {}
  78. try { lastLockoutDate = fromUser.LastLockoutDate; } catch (NotSupportedException) {}
  79. try { LastLoginDate = fromUser.LastLoginDate; } catch (NotSupportedException) {}
  80. try { lastPasswordChangedDate = fromUser.LastPasswordChangedDate; } catch (NotSupportedException) {}
  81. try { passwordQuestion = fromUser.PasswordQuestion; } catch (NotSupportedException) {}
  82. try { providerUserKey = fromUser.ProviderUserKey; } catch (NotSupportedException) {}
  83. }
  84. internal void UpdateUser ()
  85. {
  86. MembershipUser newUser = Provider.GetUser (name, false);
  87. UpdateSelf (newUser);
  88. }
  89. public virtual bool ChangePassword (string oldPassword, string newPassword)
  90. {
  91. bool success = Provider.ChangePassword (UserName, oldPassword, newPassword);
  92. UpdateUser ();
  93. return success;
  94. }
  95. public virtual bool ChangePasswordQuestionAndAnswer (string password, string newPasswordQuestion, string newPasswordAnswer)
  96. {
  97. bool success = Provider.ChangePasswordQuestionAndAnswer (UserName, password, newPasswordQuestion, newPasswordAnswer);
  98. UpdateUser ();
  99. return success;
  100. }
  101. public virtual string GetPassword ()
  102. {
  103. return GetPassword (null);
  104. }
  105. public virtual string GetPassword (string answer)
  106. {
  107. return Provider.GetPassword (UserName, answer);
  108. }
  109. public virtual string ResetPassword ()
  110. {
  111. return ResetPassword (null);
  112. }
  113. public virtual string ResetPassword (string answer)
  114. {
  115. string newPass = Provider.ResetPassword (UserName, answer);
  116. UpdateUser ();
  117. return newPass;
  118. }
  119. public virtual string Comment {
  120. get { return comment; }
  121. set { comment = value; }
  122. }
  123. public virtual DateTime CreationDate {
  124. get { return creationDate.ToLocalTime (); }
  125. }
  126. public virtual string Email {
  127. get { return email; }
  128. set { email = value; }
  129. }
  130. public virtual bool IsApproved {
  131. get { return isApproved; }
  132. set { isApproved = value; }
  133. }
  134. public virtual bool IsLockedOut {
  135. get { return isLockedOut; }
  136. }
  137. public bool IsOnline {
  138. get {
  139. return LastActivityDate > DateTime.Now - TimeSpan.FromMinutes (Membership.UserIsOnlineTimeWindow);
  140. }
  141. }
  142. public virtual DateTime LastActivityDate {
  143. get { return lastActivityDate.ToLocalTime (); }
  144. set { lastActivityDate = value.ToUniversalTime (); }
  145. }
  146. public virtual DateTime LastLoginDate {
  147. get { return lastLoginDate.ToLocalTime (); }
  148. set { lastLoginDate = value.ToUniversalTime (); }
  149. }
  150. public virtual DateTime LastPasswordChangedDate {
  151. get { return lastPasswordChangedDate.ToLocalTime (); }
  152. }
  153. public virtual DateTime LastLockoutDate {
  154. get { return lastLockoutDate.ToLocalTime (); }
  155. }
  156. public virtual string PasswordQuestion {
  157. get { return passwordQuestion; }
  158. }
  159. public virtual string ProviderName {
  160. get { return providerName; }
  161. }
  162. public virtual string UserName {
  163. get { return name; }
  164. }
  165. public virtual object ProviderUserKey {
  166. get { return providerUserKey; }
  167. }
  168. public override string ToString ()
  169. {
  170. return UserName;
  171. }
  172. public virtual bool UnlockUser ()
  173. {
  174. bool retval = Provider.UnlockUser (UserName);
  175. UpdateUser ();
  176. return retval;
  177. }
  178. MembershipProvider Provider {
  179. get {
  180. MembershipProvider p = Membership.Providers [ProviderName];
  181. if (p == null) throw new InvalidOperationException ("Membership provider '" + ProviderName + "' not found.");
  182. return p;
  183. }
  184. }
  185. }
  186. }
  187. #endif