Avoid locking in WireupAutomaticEvents
[mono.git] / mcs / class / System.Web / System.Web.Security / MembershipProvider.cs
1 //
2 // System.Web.Security.MembershipProvider
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Lluis Sanchez Gual (lluis@novell.com)
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
31 using System.ComponentModel;
32 using System.Configuration.Provider;
33 using System.Reflection;
34 using System.Runtime.CompilerServices;
35 using System.Text;
36 using System.Web.Configuration;
37
38 namespace System.Web.Security
39 {
40 #if NET_4_0
41         [TypeForwardedFrom ("System.Web, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a")]
42 #endif
43         public abstract class MembershipProvider : ProviderBase
44         {
45 #if NET_4_0
46                 const string HELPER_TYPE_NAME = "System.Web.Security.MembershipHelper, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
47
48                 internal static IMembershipHelper Helper {
49                         get { return helper; }
50                 }
51                 static IMembershipHelper helper;
52 #else
53                 static MembershipHelper helper;
54 #endif
55                 
56                 static readonly object validatingPasswordEvent = new object ();
57                 
58                 EventHandlerList events = new EventHandlerList ();
59                 public event MembershipValidatePasswordEventHandler ValidatingPassword {
60                         add { events.AddHandler (validatingPasswordEvent, value); }
61                         remove { events.RemoveHandler (validatingPasswordEvent, value); }
62                 }
63
64                 static MembershipProvider ()
65                 {
66 #if NET_4_0
67                         Type type = Type.GetType (HELPER_TYPE_NAME, false);
68                         if (type == null)
69                                 return;
70
71                         try {
72                                 helper = Activator.CreateInstance (type) as IMembershipHelper;
73                         } catch {
74                                 // ignore
75                         }
76 #else
77                         helper = new MembershipHelper ();
78 #endif
79                 }
80
81                 protected MembershipProvider ()
82                 {
83                 }
84                 
85                 public abstract bool ChangePassword (string name, string oldPwd, string newPwd);
86                 public abstract bool ChangePasswordQuestionAndAnswer (string name, string password, string newPwdQuestion, string newPwdAnswer);
87                 public abstract MembershipUser CreateUser (string username, string password, string email, string pwdQuestion, string pwdAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status);
88                 public abstract bool DeleteUser (string name, bool deleteAllRelatedData);
89                 public abstract MembershipUserCollection FindUsersByEmail (string emailToMatch, int pageIndex, int pageSize, out int totalRecords);
90                 public abstract MembershipUserCollection FindUsersByName (string nameToMatch, int pageIndex, int pageSize, out int totalRecords);
91                 public abstract MembershipUserCollection GetAllUsers (int pageIndex, int pageSize, out int totalRecords);
92                 public abstract int GetNumberOfUsersOnline ();
93                 public abstract string GetPassword (string name, string answer);
94                 public abstract MembershipUser GetUser (string name, bool userIsOnline);
95                 public abstract MembershipUser GetUser (object providerUserKey, bool userIsOnline);
96                 public abstract string GetUserNameByEmail (string email);
97                 public abstract string ResetPassword (string name, string answer);
98                 public abstract void UpdateUser (MembershipUser user);
99                 public abstract bool ValidateUser (string name, string password);
100                 public abstract bool UnlockUser (string userName);
101                 
102                 public abstract string ApplicationName { get; set; }
103                 public abstract bool EnablePasswordReset { get; }
104                 public abstract bool EnablePasswordRetrieval { get; }
105                 public abstract bool RequiresQuestionAndAnswer { get; }
106                 public abstract int MaxInvalidPasswordAttempts { get; }
107                 public abstract int MinRequiredNonAlphanumericCharacters { get; }
108                 public abstract int MinRequiredPasswordLength { get; }
109                 public abstract int PasswordAttemptWindow { get; }
110                 public abstract MembershipPasswordFormat PasswordFormat { get; }
111                 public abstract string PasswordStrengthRegularExpression { get; }
112                 public abstract bool RequiresUniqueEmail { get; }
113                 
114                 protected virtual void OnValidatingPassword (ValidatePasswordEventArgs args)
115                 {
116                         MembershipValidatePasswordEventHandler eh = events [validatingPasswordEvent] as MembershipValidatePasswordEventHandler;
117                         if (eh != null)
118                                 eh (this, args);
119                 }
120
121                 protected virtual byte [] DecryptPassword (byte [] encodedPassword)
122                 {
123 #if NET_4_0
124                         if (helper == null)
125                                 throw new PlatformNotSupportedException ("This method is not available.");
126 #endif
127                         return helper.DecryptPassword (encodedPassword);
128                 }
129
130                 protected virtual byte[] EncryptPassword (byte[] password)
131                 {
132 #if NET_4_0
133                         return EncryptPassword (password, MembershipPasswordCompatibilityMode.Framework20);
134 #else
135                         return helper.EncryptPassword (password);
136 #endif
137                 }
138 #if NET_4_0
139                 [MonoTODO ("Discover what actually is 4.0 password compatibility mode.")]
140                 protected virtual byte[] EncryptPassword (byte[] password, MembershipPasswordCompatibilityMode legacyPasswordCompatibilityMode)
141                 {
142                         if (helper == null)
143                                 throw new PlatformNotSupportedException ("This method is not available.");
144
145                         if (legacyPasswordCompatibilityMode == MembershipPasswordCompatibilityMode.Framework40)
146                                 throw new PlatformNotSupportedException ("Framework 4.0 password encryption mode is not supported at this time.");
147                         
148                         return helper.EncryptPassword (password);
149                 }
150 #endif
151         }
152 }
153
154
155