Membership.cs 10 KB

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