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