MembershipProvider.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // System.Web.Security.MembershipProvider
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // (C) 2003 Ben Maurer
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_2_0
  31. using System.Configuration.Provider;
  32. using System.Web.Configuration;
  33. using System.Security.Cryptography;
  34. using System.Text;
  35. namespace System.Web.Security
  36. {
  37. public abstract class MembershipProvider : ProviderBase
  38. {
  39. protected MembershipProvider ()
  40. {
  41. }
  42. public abstract bool ChangePassword (string name, string oldPwd, string newPwd);
  43. public abstract bool ChangePasswordQuestionAndAnswer (string name, string password, string newPwdQuestion, string newPwdAnswer);
  44. public abstract MembershipUser CreateUser (string username, string password, string email, string pwdQuestion, string pwdAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status);
  45. public abstract bool DeleteUser (string name, bool deleteAllRelatedData);
  46. public abstract MembershipUserCollection FindUsersByEmail (string emailToMatch, int pageIndex, int pageSize, out int totalRecords);
  47. public abstract MembershipUserCollection FindUsersByName (string nameToMatch, int pageIndex, int pageSize, out int totalRecords);
  48. public abstract MembershipUserCollection GetAllUsers (int pageIndex, int pageSize, out int totalRecords);
  49. public abstract int GetNumberOfUsersOnline ();
  50. public abstract string GetPassword (string name, string answer);
  51. public abstract MembershipUser GetUser (string name, bool userIsOnline);
  52. public abstract MembershipUser GetUser (object providerUserKey, bool userIsOnline);
  53. public abstract string GetUserNameByEmail (string email);
  54. public abstract string ResetPassword (string name, string answer);
  55. public abstract void UpdateUser (MembershipUser user);
  56. public abstract bool ValidateUser (string name, string password);
  57. public abstract bool UnlockUser (string userName);
  58. public abstract string ApplicationName { get; set; }
  59. public abstract bool EnablePasswordReset { get; }
  60. public abstract bool EnablePasswordRetrieval { get; }
  61. public abstract bool RequiresQuestionAndAnswer { get; }
  62. public abstract int MaxInvalidPasswordAttempts { get; }
  63. public abstract int MinRequiredNonAlphanumericCharacters { get; }
  64. public abstract int MinRequiredPasswordLength { get; }
  65. public abstract int PasswordAttemptWindow { get; }
  66. public abstract MembershipPasswordFormat PasswordFormat { get; }
  67. public abstract string PasswordStrengthRegularExpression { get; }
  68. public abstract bool RequiresUniqueEmail { get; }
  69. protected virtual void OnValidatingPassword (ValidatePasswordEventArgs args)
  70. {
  71. if (ValidatingPassword != null)
  72. ValidatingPassword (this, args);
  73. }
  74. protected virtual byte[] DecryptPassword (byte[] encodedPassword)
  75. {
  76. throw new NotImplementedException ();
  77. }
  78. protected virtual byte[] EncryptPassword (byte[] password)
  79. {
  80. throw new NotImplementedException ();
  81. }
  82. public event MembershipValidatePasswordEventHandler ValidatingPassword;
  83. internal string EncodePassword (string password, MembershipPasswordFormat passwordFormat, string salt)
  84. {
  85. byte[] password_bytes;
  86. byte[] salt_bytes;
  87. switch (passwordFormat) {
  88. case MembershipPasswordFormat.Clear:
  89. return password;
  90. case MembershipPasswordFormat.Hashed:
  91. password_bytes = Encoding.Unicode.GetBytes (password);
  92. salt_bytes = Convert.FromBase64String (salt);
  93. byte[] hashBytes = new byte[salt_bytes.Length + password_bytes.Length];
  94. Buffer.BlockCopy (salt_bytes, 0, hashBytes, 0, salt_bytes.Length);
  95. Buffer.BlockCopy (password_bytes, 0, hashBytes, salt_bytes.Length, password_bytes.Length);
  96. MembershipSection section = (MembershipSection)WebConfigurationManager.GetSection ("system.web/membership");
  97. string alg_type = section.HashAlgorithmType;
  98. if (alg_type == "") {
  99. MachineKeySection keysection = (MachineKeySection)WebConfigurationManager.GetSection ("system.web/machineKey");
  100. alg_type = keysection.Validation.ToString ();
  101. }
  102. using (HashAlgorithm hash = HashAlgorithm.Create (alg_type)) {
  103. hash.TransformFinalBlock (hashBytes, 0, hashBytes.Length);
  104. return Convert.ToBase64String (hash.Hash);
  105. }
  106. case MembershipPasswordFormat.Encrypted:
  107. password_bytes = Encoding.Unicode.GetBytes (password);
  108. salt_bytes = Convert.FromBase64String (salt);
  109. byte[] buf = new byte[password_bytes.Length + salt_bytes.Length];
  110. Array.Copy (salt_bytes, 0, buf, 0, salt_bytes.Length);
  111. Array.Copy (password_bytes, 0, buf, salt_bytes.Length, password_bytes.Length);
  112. return Convert.ToBase64String (EncryptPassword (buf));
  113. default:
  114. /* not reached.. */
  115. return null;
  116. }
  117. }
  118. internal string DecodePassword (string password, MembershipPasswordFormat passwordFormat)
  119. {
  120. switch (passwordFormat) {
  121. case MembershipPasswordFormat.Clear:
  122. return password;
  123. case MembershipPasswordFormat.Hashed:
  124. throw new ProviderException ("Hashed passwords cannot be decoded.");
  125. case MembershipPasswordFormat.Encrypted:
  126. return Encoding.Unicode.GetString (DecryptPassword (Convert.FromBase64String (password)));
  127. default:
  128. /* not reached.. */
  129. return null;
  130. }
  131. }
  132. }
  133. }
  134. #endif