MembershipProvider.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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-2010 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. using System.ComponentModel;
  31. using System.Configuration.Provider;
  32. using System.Reflection;
  33. using System.Runtime.CompilerServices;
  34. using System.Text;
  35. using System.Web.Configuration;
  36. namespace System.Web.Security
  37. {
  38. [TypeForwardedFrom ("System.Web, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a")]
  39. public abstract class MembershipProvider : ProviderBase
  40. {
  41. const string HELPER_TYPE_NAME = "System.Web.Security.MembershipHelper, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
  42. internal static IMembershipHelper Helper {
  43. get { return helper; }
  44. }
  45. static IMembershipHelper helper;
  46. static readonly object validatingPasswordEvent = new object ();
  47. EventHandlerList events = new EventHandlerList ();
  48. public event MembershipValidatePasswordEventHandler ValidatingPassword {
  49. add { events.AddHandler (validatingPasswordEvent, value); }
  50. remove { events.RemoveHandler (validatingPasswordEvent, value); }
  51. }
  52. static MembershipProvider ()
  53. {
  54. Type type = Type.GetType (HELPER_TYPE_NAME, false);
  55. if (type == null)
  56. return;
  57. try {
  58. helper = Activator.CreateInstance (type) as IMembershipHelper;
  59. } catch {
  60. // ignore
  61. }
  62. }
  63. protected MembershipProvider ()
  64. {
  65. }
  66. public abstract bool ChangePassword (string name, string oldPwd, string newPwd);
  67. public abstract bool ChangePasswordQuestionAndAnswer (string name, string password, string newPwdQuestion, string newPwdAnswer);
  68. public abstract MembershipUser CreateUser (string username, string password, string email, string pwdQuestion, string pwdAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status);
  69. public abstract bool DeleteUser (string name, bool deleteAllRelatedData);
  70. public abstract MembershipUserCollection FindUsersByEmail (string emailToMatch, int pageIndex, int pageSize, out int totalRecords);
  71. public abstract MembershipUserCollection FindUsersByName (string nameToMatch, int pageIndex, int pageSize, out int totalRecords);
  72. public abstract MembershipUserCollection GetAllUsers (int pageIndex, int pageSize, out int totalRecords);
  73. public abstract int GetNumberOfUsersOnline ();
  74. public abstract string GetPassword (string name, string answer);
  75. public abstract MembershipUser GetUser (string name, bool userIsOnline);
  76. public abstract MembershipUser GetUser (object providerUserKey, bool userIsOnline);
  77. public abstract string GetUserNameByEmail (string email);
  78. public abstract string ResetPassword (string name, string answer);
  79. public abstract void UpdateUser (MembershipUser user);
  80. public abstract bool ValidateUser (string name, string password);
  81. public abstract bool UnlockUser (string userName);
  82. public abstract string ApplicationName { get; set; }
  83. public abstract bool EnablePasswordReset { get; }
  84. public abstract bool EnablePasswordRetrieval { get; }
  85. public abstract bool RequiresQuestionAndAnswer { get; }
  86. public abstract int MaxInvalidPasswordAttempts { get; }
  87. public abstract int MinRequiredNonAlphanumericCharacters { get; }
  88. public abstract int MinRequiredPasswordLength { get; }
  89. public abstract int PasswordAttemptWindow { get; }
  90. public abstract MembershipPasswordFormat PasswordFormat { get; }
  91. public abstract string PasswordStrengthRegularExpression { get; }
  92. public abstract bool RequiresUniqueEmail { get; }
  93. protected virtual void OnValidatingPassword (ValidatePasswordEventArgs args)
  94. {
  95. MembershipValidatePasswordEventHandler eh = events [validatingPasswordEvent] as MembershipValidatePasswordEventHandler;
  96. if (eh != null)
  97. eh (this, args);
  98. }
  99. protected virtual byte [] DecryptPassword (byte [] encodedPassword)
  100. {
  101. if (helper == null)
  102. throw new PlatformNotSupportedException ("This method is not available.");
  103. return helper.DecryptPassword (encodedPassword);
  104. }
  105. protected virtual byte[] EncryptPassword (byte[] password)
  106. {
  107. return EncryptPassword (password, MembershipPasswordCompatibilityMode.Framework20);
  108. }
  109. [MonoTODO ("Discover what actually is 4.0 password compatibility mode.")]
  110. protected virtual byte[] EncryptPassword (byte[] password, MembershipPasswordCompatibilityMode legacyPasswordCompatibilityMode)
  111. {
  112. if (helper == null)
  113. throw new PlatformNotSupportedException ("This method is not available.");
  114. if (legacyPasswordCompatibilityMode == MembershipPasswordCompatibilityMode.Framework40)
  115. throw new PlatformNotSupportedException ("Framework 4.0 password encryption mode is not supported at this time.");
  116. return helper.EncryptPassword (password);
  117. }
  118. }
  119. }