Membership.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. using System.Security.Cryptography;
  38. namespace System.Web.Security
  39. {
  40. public static class Membership
  41. {
  42. static MembershipProviderCollection providers;
  43. static MembershipProvider provider;
  44. static int onlineTimeWindow;
  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. provider = providers[section.DefaultProvider];
  51. onlineTimeWindow = (int) section.UserIsOnlineTimeWindow.TotalMinutes;
  52. }
  53. public static MembershipUser CreateUser (string username, string password)
  54. {
  55. return CreateUser (username, password, null);
  56. }
  57. public static MembershipUser CreateUser (string username, string password, string email)
  58. {
  59. MembershipCreateStatus status;
  60. MembershipUser usr = CreateUser (username, password, email, null, null, true, out status);
  61. if (usr == null)
  62. throw new MembershipCreateUserException (status);
  63. return usr;
  64. }
  65. public static MembershipUser CreateUser (string username, string password, string email, string pwdQuestion, string pwdAnswer, bool isApproved, out MembershipCreateStatus status)
  66. {
  67. return Provider.CreateUser (username, password, email, pwdQuestion, pwdAnswer, isApproved, null, out status);
  68. }
  69. public static MembershipUser CreateUser (string username, string password, string email, string pwdQuestion, string pwdAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
  70. {
  71. return Provider.CreateUser (username, password, email, pwdQuestion, pwdAnswer, isApproved, providerUserKey, out status);
  72. }
  73. public static bool DeleteUser (string username)
  74. {
  75. return Provider.DeleteUser (username, true);
  76. }
  77. public static bool DeleteUser (string username, bool deleteAllRelatedData)
  78. {
  79. return Provider.DeleteUser (username, deleteAllRelatedData);
  80. }
  81. public static string GeneratePassword (int length, int numberOfNonAlphanumericCharacters)
  82. {
  83. RandomNumberGenerator rng = RandomNumberGenerator.Create ();
  84. byte[] pass_bytes = new byte[length];
  85. int i;
  86. int num_nonalpha = 0;
  87. rng.GetBytes (pass_bytes);
  88. for (i = 0; i < length; i ++) {
  89. /* convert the random bytes to ascii values 33-126 */
  90. pass_bytes[i] = (byte)(pass_bytes[i] % 93 + 33);
  91. /* and count the number of
  92. * non-alphanumeric characters we have
  93. * as we go */
  94. if ((pass_bytes[i] >= 33 && pass_bytes[i] <= 47)
  95. || (pass_bytes[i] >= 58 && pass_bytes[i] <= 64)
  96. || (pass_bytes[i] >= 91 && pass_bytes[i] <= 96)
  97. || (pass_bytes[i] >= 123 && pass_bytes[i] <= 126))
  98. num_nonalpha++;
  99. /* get rid of any quotes in the
  100. * password, just in case they cause
  101. * problems */
  102. if (pass_bytes[i] == 34 || pass_bytes[i] == 39)
  103. pass_bytes[i] ++;
  104. else if (pass_bytes[i] == 96)
  105. pass_bytes[i] --;
  106. }
  107. if (num_nonalpha < numberOfNonAlphanumericCharacters) {
  108. /* loop over the array, converting the
  109. * least number of alphanumeric
  110. * characters to non-alpha */
  111. for (i = 0; i < length; i ++) {
  112. if (num_nonalpha == numberOfNonAlphanumericCharacters)
  113. break;
  114. if (pass_bytes[i] >= 48 && pass_bytes[i] <= 57) {
  115. pass_bytes[i] = (byte)(pass_bytes[i] - 48 + 33);
  116. num_nonalpha++;
  117. }
  118. else if (pass_bytes[i] >= 65 && pass_bytes[i] <= 90) {
  119. pass_bytes[i] = (byte)((pass_bytes[i] - 65) % 13 + 33);
  120. num_nonalpha++;
  121. }
  122. else if (pass_bytes[i] >= 97 && pass_bytes[i] <= 122) {
  123. pass_bytes[i] = (byte)((pass_bytes[i] - 97) % 13 + 33);
  124. num_nonalpha++;
  125. }
  126. /* and make sure we don't end up with quote characters */
  127. if (pass_bytes[i] == 34 || pass_bytes[i] == 39)
  128. pass_bytes[i]++;
  129. else if (pass_bytes[i] == 96)
  130. pass_bytes[i] --;
  131. }
  132. }
  133. return Encoding.ASCII.GetString (pass_bytes);
  134. }
  135. public static MembershipUserCollection GetAllUsers ()
  136. {
  137. int total;
  138. return GetAllUsers (1, int.MaxValue, out total);
  139. }
  140. public static MembershipUserCollection GetAllUsers (int pageIndex, int pageSize, out int totalRecords)
  141. {
  142. return Provider.GetAllUsers (pageIndex, pageSize, out totalRecords);
  143. }
  144. public static int GetNumberOfUsersOnline ()
  145. {
  146. return Provider.GetNumberOfUsersOnline ();
  147. }
  148. public static MembershipUser GetUser ()
  149. {
  150. return GetUser (HttpContext.Current.User.Identity.Name, true);
  151. }
  152. public static MembershipUser GetUser (bool userIsOnline)
  153. {
  154. return GetUser (HttpContext.Current.User.Identity.Name, userIsOnline);
  155. }
  156. public static MembershipUser GetUser (string username)
  157. {
  158. return GetUser (username, false);
  159. }
  160. public static MembershipUser GetUser (string username, bool userIsOnline)
  161. {
  162. return Provider.GetUser (username, userIsOnline);
  163. }
  164. public static MembershipUser GetUser (object providerUserKey)
  165. {
  166. return GetUser (providerUserKey, false);
  167. }
  168. public static MembershipUser GetUser (object providerUserKey, bool userIsOnline)
  169. {
  170. return Provider.GetUser (providerUserKey, userIsOnline);
  171. }
  172. public static string GetUserNameByEmail (string email)
  173. {
  174. return Provider.GetUserNameByEmail (email);
  175. }
  176. public static void UpdateUser (MembershipUser user)
  177. {
  178. Provider.UpdateUser (user);
  179. }
  180. public static bool ValidateUser (string username, string password)
  181. {
  182. return Provider.ValidateUser (username, password);
  183. }
  184. public static MembershipUserCollection FindUsersByEmail (string emailToMatch)
  185. {
  186. int totalRecords;
  187. return Provider.FindUsersByEmail (emailToMatch, 0, int.MaxValue, out totalRecords);
  188. }
  189. public static MembershipUserCollection FindUsersByEmail (string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
  190. {
  191. return Provider.FindUsersByEmail (emailToMatch, pageIndex, pageSize, out totalRecords);
  192. }
  193. public static MembershipUserCollection FindUsersByName (string nameToMatch)
  194. {
  195. int totalRecords;
  196. return Provider.FindUsersByName (nameToMatch, 0, int.MaxValue, out totalRecords);
  197. }
  198. public static MembershipUserCollection FindUsersByName (string nameToMatch, int pageIndex, int pageSize, out int totalRecords)
  199. {
  200. return Provider.FindUsersByName (nameToMatch, pageIndex, pageSize, out totalRecords);
  201. }
  202. public static string ApplicationName {
  203. get { return Provider.ApplicationName; }
  204. set { Provider.ApplicationName = value; }
  205. }
  206. public static bool EnablePasswordReset {
  207. get { return Provider.EnablePasswordReset; }
  208. }
  209. public static bool EnablePasswordRetrieval {
  210. get { return Provider.EnablePasswordRetrieval; }
  211. }
  212. public static bool RequiresQuestionAndAnswer {
  213. get { return Provider.RequiresQuestionAndAnswer; }
  214. }
  215. public static int MaxInvalidPasswordAttempts {
  216. get { return Provider.MaxInvalidPasswordAttempts; }
  217. }
  218. public static int MinRequiredNonAlphanumericCharacters {
  219. get { return Provider.MinRequiredNonAlphanumericCharacters; }
  220. }
  221. public static int MinRequiredPasswordLength {
  222. get { return Provider.MinRequiredPasswordLength; }
  223. }
  224. public static int PasswordAttemptWindow {
  225. get { return Provider.PasswordAttemptWindow; }
  226. }
  227. public static string PasswordStrengthRegularExpression {
  228. get { return Provider.PasswordStrengthRegularExpression; }
  229. }
  230. public static MembershipProvider Provider {
  231. get { return provider; }
  232. }
  233. public static MembershipProviderCollection Providers {
  234. get { return providers; }
  235. }
  236. public static int UserIsOnlineTimeWindow {
  237. get { return onlineTimeWindow; }
  238. }
  239. public static event MembershipValidatePasswordEventHandler ValidatingPassword {
  240. add { Provider.ValidatingPassword += value; }
  241. remove { Provider.ValidatingPassword -= value; }
  242. }
  243. }
  244. }
  245. #endif