Membership.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // System.Web.Security.Membership
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // (C) 2003 Ben Maurer
  9. // (C) 2005 Novell, inc.
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Text;
  35. using System.Web.Configuration;
  36. using System.Configuration;
  37. namespace System.Web.Security
  38. {
  39. public static class Membership
  40. {
  41. static MembershipProviderCollection providers;
  42. static MembershipProvider provider;
  43. static int onlineTimeWindow;
  44. [MonoTODO]
  45. static Membership ()
  46. {
  47. MembershipSection section = (MembershipSection) WebConfigurationManager.GetSection ("system.web/membership");
  48. providers = new MembershipProviderCollection ();
  49. ProvidersHelper.InstantiateProviders (section.Providers, providers, typeof (MembershipProvider));
  50. /* do we add this fallback? */
  51. if (providers.Count == 0) {
  52. provider = new SqlMembershipProvider ();
  53. NameValueCollection attributes = new NameValueCollection ();
  54. provider.Initialize ("AspNetSqlMembershipProvider", attributes);
  55. providers.Add (provider);
  56. }
  57. provider = providers[section.DefaultProvider];
  58. onlineTimeWindow = (int) section.UserIsOnlineTimeWindow.TotalMinutes;
  59. }
  60. public static MembershipUser CreateUser (string username, string password)
  61. {
  62. return CreateUser (username, password, null);
  63. }
  64. public static MembershipUser CreateUser (string username, string password, string email)
  65. {
  66. MembershipCreateStatus status;
  67. MembershipUser usr = CreateUser (username, password, email, null, null, true, out status);
  68. if (usr == null)
  69. throw new MembershipCreateUserException (status);
  70. return usr;
  71. }
  72. public static MembershipUser CreateUser (string username, string password, string email, string pwdQuestion, string pwdAnswer, bool isApproved, out MembershipCreateStatus status)
  73. {
  74. return Provider.CreateUser (username, password, email, pwdQuestion, pwdAnswer, isApproved, null, out status);
  75. }
  76. public static MembershipUser CreateUser (string username, string password, string email, string pwdQuestion, string pwdAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
  77. {
  78. return Provider.CreateUser (username, password, email, pwdQuestion, pwdAnswer, isApproved, providerUserKey, out status);
  79. }
  80. public static bool DeleteUser (string username)
  81. {
  82. return Provider.DeleteUser (username, true);
  83. }
  84. public static bool DeleteUser (string username, bool deleteAllRelatedData)
  85. {
  86. return Provider.DeleteUser (username, deleteAllRelatedData);
  87. }
  88. [MonoTODO]
  89. public static string GeneratePassword (int length, int numberOfNonAlphanumericCharacters)
  90. {
  91. throw new NotImplementedException ();
  92. }
  93. public static MembershipUserCollection GetAllUsers ()
  94. {
  95. int total;
  96. return GetAllUsers (1, int.MaxValue, out total);
  97. }
  98. public static MembershipUserCollection GetAllUsers (int pageIndex, int pageSize, out int totalRecords)
  99. {
  100. return Provider.GetAllUsers (pageIndex, pageSize, out totalRecords);
  101. }
  102. public static int GetNumberOfUsersOnline ()
  103. {
  104. return Provider.GetNumberOfUsersOnline ();
  105. }
  106. public static MembershipUser GetUser ()
  107. {
  108. return GetUser (HttpContext.Current.User.Identity.Name, true);
  109. }
  110. public static MembershipUser GetUser (bool userIsOnline)
  111. {
  112. return GetUser (HttpContext.Current.User.Identity.Name, userIsOnline);
  113. }
  114. public static MembershipUser GetUser (string username)
  115. {
  116. return GetUser (username, false);
  117. }
  118. public static MembershipUser GetUser (string username, bool userIsOnline)
  119. {
  120. return Provider.GetUser (username, userIsOnline);
  121. }
  122. public static MembershipUser GetUser (object providerUserKey)
  123. {
  124. return GetUser (providerUserKey, false);
  125. }
  126. public static MembershipUser GetUser (object providerUserKey, bool userIsOnline)
  127. {
  128. return Provider.GetUser (providerUserKey, userIsOnline);
  129. }
  130. public static string GetUserNameByEmail (string email)
  131. {
  132. return Provider.GetUserNameByEmail (email);
  133. }
  134. public static void UpdateUser (MembershipUser user)
  135. {
  136. Provider.UpdateUser (user);
  137. }
  138. public static bool ValidateUser (string username, string password)
  139. {
  140. return Provider.ValidateUser (username, password);
  141. }
  142. public static MembershipUserCollection FindUsersByEmail (string emailToMatch)
  143. {
  144. int totalRecords;
  145. return Provider.FindUsersByEmail (emailToMatch, 0, int.MaxValue, out totalRecords);
  146. }
  147. public static MembershipUserCollection FindUsersByEmail (string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
  148. {
  149. return Provider.FindUsersByEmail (emailToMatch, pageIndex, pageSize, out totalRecords);
  150. }
  151. public static MembershipUserCollection FindUsersByName (string nameToMatch)
  152. {
  153. int totalRecords;
  154. return Provider.FindUsersByName (nameToMatch, 0, int.MaxValue, out totalRecords);
  155. }
  156. public static MembershipUserCollection FindUsersByName (string nameToMatch, int pageIndex, int pageSize, out int totalRecords)
  157. {
  158. return Provider.FindUsersByName (nameToMatch, pageIndex, pageSize, out totalRecords);
  159. }
  160. public static string ApplicationName {
  161. get { return Provider.ApplicationName; }
  162. set { Provider.ApplicationName = value; }
  163. }
  164. public static bool EnablePasswordReset {
  165. get { return Provider.EnablePasswordReset; }
  166. }
  167. public static bool EnablePasswordRetrieval {
  168. get { return Provider.EnablePasswordRetrieval; }
  169. }
  170. public static bool RequiresQuestionAndAnswer {
  171. get { return Provider.RequiresQuestionAndAnswer; }
  172. }
  173. public static int MaxInvalidPasswordAttempts {
  174. get { return Provider.MaxInvalidPasswordAttempts; }
  175. }
  176. public static int MinRequiredNonAlphanumericCharacters {
  177. get { return Provider.MinRequiredNonAlphanumericCharacters; }
  178. }
  179. public static int MinRequiredPasswordLength {
  180. get { return Provider.MinRequiredPasswordLength; }
  181. }
  182. public static int PasswordAttemptWindow {
  183. get { return Provider.PasswordAttemptWindow; }
  184. }
  185. public static string PasswordStrengthRegularExpression {
  186. get { return Provider.PasswordStrengthRegularExpression; }
  187. }
  188. public static MembershipProvider Provider {
  189. get { return provider; }
  190. }
  191. public static MembershipProviderCollection Providers {
  192. get { return providers; }
  193. }
  194. public static int UserIsOnlineTimeWindow {
  195. get { return onlineTimeWindow; }
  196. }
  197. [MonoTODO ("Fire it")]
  198. public static event MembershipValidatePasswordEventHandler ValidatingPassword {
  199. add { Provider.ValidatingPassword += value; }
  200. remove { Provider.ValidatingPassword -= value; }
  201. }
  202. }
  203. }
  204. #endif