Membership.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. static Membership ()
  45. {
  46. System.Configuration.Configuration conf = WebConfigurationManager.OpenWebConfiguration ("/");
  47. MembershipSection section = (MembershipSection) conf.GetSection ("system.web/membership");
  48. providers = new MembershipProviderCollection ();
  49. foreach (ProviderSettings prov in section.Providers) {
  50. Type t = Type.GetType (prov.Type);
  51. if (t == null)
  52. throw new ConfigurationException ("Cannot find type: " + prov.Type);
  53. if (!typeof(MembershipProvider).IsAssignableFrom (t))
  54. throw new ConfigurationException ("The provided type is not a MembershipProvider subclass: " + prov.Type);
  55. MembershipProvider pr = (MembershipProvider) Activator.CreateInstance (t);
  56. pr.Initialize (prov.Name, prov.Parameters);
  57. if (provider == null || prov.Name == section.DefaultProvider)
  58. provider = pr;
  59. providers.Add (pr);
  60. }
  61. if (providers.Count == 0) {
  62. provider = new SqlMembershipProvider ();
  63. NameValueCollection attributes = new NameValueCollection ();
  64. provider.Initialize ("AspNetSqlMembershipProvider", attributes);
  65. providers.Add (provider);
  66. }
  67. onlineTimeWindow = (int) section.UserIsOnlineTimeWindow.TotalMinutes;
  68. }
  69. public static MembershipUser CreateUser (string username, string password)
  70. {
  71. return CreateUser (username, password, null);
  72. }
  73. public static MembershipUser CreateUser (string username, string password, string email)
  74. {
  75. MembershipCreateStatus status;
  76. MembershipUser usr = CreateUser (username, password, email, null, null, true, out status);
  77. if (usr == null)
  78. throw new MembershipCreateUserException (status);
  79. return usr;
  80. }
  81. public static MembershipUser CreateUser (string username, string password, string email, string pwdQuestion, string pwdAnswer, bool isApproved, out MembershipCreateStatus status)
  82. {
  83. return Provider.CreateUser (username, password, email, pwdQuestion, pwdAnswer, isApproved, null, out status);
  84. }
  85. public static MembershipUser CreateUser (string username, string password, string email, string pwdQuestion, string pwdAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
  86. {
  87. return Provider.CreateUser (username, password, email, pwdQuestion, pwdAnswer, isApproved, providerUserKey, out status);
  88. }
  89. public static bool DeleteUser (string username)
  90. {
  91. return Provider.DeleteUser (username, true);
  92. }
  93. public static bool DeleteUser (string username, bool deleteAllRelatedData)
  94. {
  95. return Provider.DeleteUser (username, deleteAllRelatedData);
  96. }
  97. [MonoTODO]
  98. public static string GeneratePassword (int length, int numberOfNonAlphanumericCharacters)
  99. {
  100. throw new NotImplementedException ();
  101. }
  102. public static MembershipUserCollection GetAllUsers ()
  103. {
  104. int total;
  105. return GetAllUsers (1, int.MaxValue, out total);
  106. }
  107. public static MembershipUserCollection GetAllUsers (int pageIndex, int pageSize, out int totalRecords)
  108. {
  109. return Provider.GetAllUsers (pageIndex, pageSize, out totalRecords);
  110. }
  111. public static int GetNumberOfUsersOnline ()
  112. {
  113. return Provider.GetNumberOfUsersOnline ();
  114. }
  115. public static MembershipUser GetUser ()
  116. {
  117. return GetUser (HttpContext.Current.User.Identity.Name, true);
  118. }
  119. public static MembershipUser GetUser (bool userIsOnline)
  120. {
  121. return GetUser (HttpContext.Current.User.Identity.Name, userIsOnline);
  122. }
  123. public static MembershipUser GetUser (string username)
  124. {
  125. return GetUser (username, false);
  126. }
  127. public static MembershipUser GetUser (string username, bool userIsOnline)
  128. {
  129. return Provider.GetUser (username, userIsOnline);
  130. }
  131. public static MembershipUser GetUser (object providerUserKey)
  132. {
  133. return GetUser (providerUserKey, false);
  134. }
  135. public static MembershipUser GetUser (object providerUserKey, bool userIsOnline)
  136. {
  137. return Provider.GetUser (providerUserKey, userIsOnline);
  138. }
  139. public static string GetUserNameByEmail (string email)
  140. {
  141. return Provider.GetUserNameByEmail (email);
  142. }
  143. public static void UpdateUser (MembershipUser user)
  144. {
  145. Provider.UpdateUser (user);
  146. }
  147. public static bool ValidateUser (string username, string password)
  148. {
  149. return Provider.ValidateUser (username, password);
  150. }
  151. public static MembershipUserCollection FindUsersByEmail (string emailToMatch)
  152. {
  153. int totalRecords;
  154. return Provider.FindUsersByEmail (emailToMatch, 0, int.MaxValue, out totalRecords);
  155. }
  156. public static MembershipUserCollection FindUsersByEmail (string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
  157. {
  158. return Provider.FindUsersByEmail (emailToMatch, pageIndex, pageSize, out totalRecords);
  159. }
  160. public static MembershipUserCollection FindUsersByName (string nameToMatch)
  161. {
  162. int totalRecords;
  163. return Provider.FindUsersByName (nameToMatch, 0, int.MaxValue, out totalRecords);
  164. }
  165. public static MembershipUserCollection FindUsersByName (string nameToMatch, int pageIndex, int pageSize, out int totalRecords)
  166. {
  167. return Provider.FindUsersByName (nameToMatch, pageIndex, pageSize, out totalRecords);
  168. }
  169. public static string ApplicationName {
  170. get { return Provider.ApplicationName; }
  171. set { Provider.ApplicationName = value; }
  172. }
  173. public static bool EnablePasswordReset {
  174. get { return Provider.EnablePasswordReset; }
  175. }
  176. public static bool EnablePasswordRetrieval {
  177. get { return Provider.EnablePasswordRetrieval; }
  178. }
  179. public static bool RequiresQuestionAndAnswer {
  180. get { return Provider.RequiresQuestionAndAnswer; }
  181. }
  182. public static int MaxInvalidPasswordAttempts {
  183. get { return Provider.MaxInvalidPasswordAttempts; }
  184. }
  185. public static int MinRequiredNonAlphanumericCharacters {
  186. get { return Provider.MinRequiredNonAlphanumericCharacters; }
  187. }
  188. public static int MinRequiredPasswordLength {
  189. get { return Provider.MinRequiredPasswordLength; }
  190. }
  191. public static int PasswordAttemptWindow {
  192. get { return Provider.PasswordAttemptWindow; }
  193. }
  194. public static string PasswordStrengthRegularExpression {
  195. get { return Provider.PasswordStrengthRegularExpression; }
  196. }
  197. public static MembershipProvider Provider {
  198. get { return provider; }
  199. }
  200. public static MembershipProviderCollection Providers {
  201. get { return providers; }
  202. }
  203. public static int UserIsOnlineTimeWindow {
  204. get { return onlineTimeWindow; }
  205. }
  206. [MonoTODO ("Fire it")]
  207. public static event MembershipValidatePasswordEventHandler ValidatingPassword {
  208. add { Provider.ValidatingPassword += value; }
  209. remove { Provider.ValidatingPassword -= value; }
  210. }
  211. }
  212. }
  213. #endif