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