2008-02-25 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.Security / Membership.cs
1 //
2 // System.Web.Security.Membership
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Lluis Sanchez Gual (lluis@novell.com)
7 //
8 // (C) 2003 Ben Maurer
9 // (C) 2005 Novell, inc.
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 #if NET_2_0
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Text;
37 using System.Web.Configuration;
38 using System.Configuration;
39 using System.Security.Cryptography;
40
41 namespace System.Web.Security
42 {
43         public static class Membership
44         {
45 #if TARGET_J2EE
46                 const string Membership_providers = "Membership.providers";
47                 static MembershipProviderCollection providers {
48                         get {
49                                 object o = AppDomain.CurrentDomain.GetData (Membership_providers);
50                                 if (o == null) {
51                                         lock (AppDomain.CurrentDomain) {
52                                                 o = AppDomain.CurrentDomain.GetData (Membership_providers);
53                                                 if (o == null) {
54                                                         MembershipSection section = (MembershipSection) WebConfigurationManager.GetSection ("system.web/membership");
55                                                         MembershipProviderCollection local_providers = new MembershipProviderCollection ();
56                                                         ProvidersHelper.InstantiateProviders (section.Providers, local_providers, typeof (MembershipProvider));
57                                                         AppDomain.CurrentDomain.SetData (Membership_providers, local_providers);
58                                                         o = local_providers;
59                                                 }
60                                         }
61                                 }
62
63                                 return (MembershipProviderCollection) o;
64                         }
65                 }
66                 static MembershipProvider provider {
67                         get {
68                                 MembershipSection section = (MembershipSection) WebConfigurationManager.GetSection ("system.web/membership");
69                                 MembershipProvider p = providers [section.DefaultProvider];
70                                 if (p == null)
71                                         throw new ConfigurationErrorsException ("Default Membership Provider could not be found: Cannot instantiate provider: '" + section.DefaultProvider + "'.");
72                                 return p;
73                         }
74                 }
75                 static int onlineTimeWindow {
76                         get {
77                                 MembershipSection section = (MembershipSection) WebConfigurationManager.GetSection ("system.web/membership");
78                                 return (int) section.UserIsOnlineTimeWindow.TotalMinutes;
79                         }
80                 }
81 #else
82                 static MembershipProviderCollection providers;
83                 static MembershipProvider provider;
84                 static int onlineTimeWindow;
85
86                 static Membership ()
87                 {
88                         MembershipSection section = (MembershipSection) WebConfigurationManager.GetSection ("system.web/membership");
89
90                         providers = new MembershipProviderCollection ();
91
92                         ProvidersHelper.InstantiateProviders (section.Providers, providers, typeof (MembershipProvider));
93
94                         provider = providers[section.DefaultProvider];
95
96                         onlineTimeWindow = (int) section.UserIsOnlineTimeWindow.TotalMinutes;
97                 }
98 #endif
99
100                 public static MembershipUser CreateUser (string username, string password)
101                 {
102                         return CreateUser (username, password, null);
103                 }
104                 
105                 public static MembershipUser CreateUser (string username, string password, string email)
106                 {
107                         MembershipCreateStatus status;
108                         MembershipUser usr = CreateUser (username, password, email, null, null, true, out status);
109                         if (usr == null)
110                                 throw new MembershipCreateUserException (status);
111                         
112                         return usr;
113                 }
114                 
115                 public static MembershipUser CreateUser (string username, string password, string email, string pwdQuestion, string pwdAnswer, bool isApproved, out MembershipCreateStatus status)
116                 {
117                         return Provider.CreateUser (username, password, email, pwdQuestion, pwdAnswer, isApproved, null, out status);
118                 }
119                 
120                 public static MembershipUser CreateUser (string username, string password, string email, string pwdQuestion, string pwdAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
121                 {
122                         return Provider.CreateUser (username, password, email, pwdQuestion, pwdAnswer, isApproved, providerUserKey, out status);
123                 }
124                 
125                 public static bool DeleteUser (string username)
126                 {
127                         return Provider.DeleteUser (username, true);
128                 }
129                 
130                 public static bool DeleteUser (string username, bool deleteAllRelatedData)
131                 {
132                         return Provider.DeleteUser (username, deleteAllRelatedData);
133                 }
134                 
135                 public static string GeneratePassword (int length, int numberOfNonAlphanumericCharacters)
136                 {
137                         RandomNumberGenerator rng = RandomNumberGenerator.Create ();
138                         byte[] pass_bytes = new byte[length];
139                         int i;
140                         int num_nonalpha = 0;
141
142                         rng.GetBytes (pass_bytes);
143                         
144                         for (i = 0; i < length; i ++) {
145                                 /* convert the random bytes to ascii values 33-126 */
146                                 pass_bytes[i] = (byte)(pass_bytes[i] % 93 + 33);
147
148                                 /* and count the number of
149                                  * non-alphanumeric characters we have
150                                  * as we go */
151                                 if ((pass_bytes[i] >= 33 && pass_bytes[i] <= 47)
152                                     || (pass_bytes[i] >= 58 && pass_bytes[i] <= 64)
153                                     || (pass_bytes[i] >= 91 && pass_bytes[i] <= 96)
154                                     || (pass_bytes[i] >= 123 && pass_bytes[i] <= 126))
155                                         num_nonalpha++;
156
157                                 /* get rid of any quotes in the
158                                  * password, just in case they cause
159                                  * problems */
160                                 if (pass_bytes[i] == 34 || pass_bytes[i] == 39)
161                                         pass_bytes[i] ++;
162                                 else if (pass_bytes[i] == 96)
163                                         pass_bytes[i] --;
164                         }
165
166                         if (num_nonalpha < numberOfNonAlphanumericCharacters) {
167                                 /* loop over the array, converting the
168                                  * least number of alphanumeric
169                                  * characters to non-alpha */
170                                 for (i = 0; i < length; i ++) {
171                                         if (num_nonalpha == numberOfNonAlphanumericCharacters)
172                                                 break;
173                                         if (pass_bytes[i] >= 48 && pass_bytes[i] <= 57) {
174                                                 pass_bytes[i] = (byte)(pass_bytes[i] - 48 + 33);
175                                                 num_nonalpha++;
176                                         }
177                                         else if (pass_bytes[i] >= 65 && pass_bytes[i] <= 90) {
178                                                 pass_bytes[i] = (byte)((pass_bytes[i] - 65) % 13 + 33);
179                                                 num_nonalpha++;
180                                         }
181                                         else if (pass_bytes[i] >= 97 && pass_bytes[i] <= 122) {
182                                                 pass_bytes[i] = (byte)((pass_bytes[i] - 97) % 13 + 33);
183                                                 num_nonalpha++;
184                                         }
185
186                                         /* and make sure we don't end up with quote characters */
187                                         if (pass_bytes[i] == 34 || pass_bytes[i] == 39)
188                                                 pass_bytes[i]++;
189                                         else if (pass_bytes[i] == 96)
190                                                 pass_bytes[i] --;
191                                 }
192                         }
193
194                         return Encoding.ASCII.GetString (pass_bytes);
195                 }
196                 
197                 public static MembershipUserCollection GetAllUsers ()
198                 {
199                         int total;
200                         return GetAllUsers (0, int.MaxValue, out total);
201                 }
202                 
203                 public static MembershipUserCollection GetAllUsers (int pageIndex, int pageSize, out int totalRecords)
204                 {
205                         return Provider.GetAllUsers (pageIndex, pageSize, out totalRecords);
206                 }
207                 
208                 public static int GetNumberOfUsersOnline ()
209                 {
210                         return Provider.GetNumberOfUsersOnline ();
211                 }
212                 
213                 public static MembershipUser GetUser ()
214                 {
215                         return GetUser (HttpContext.Current.User.Identity.Name, true);
216                 }
217                 
218                 public static MembershipUser GetUser (bool userIsOnline)
219                 {
220                         return GetUser (HttpContext.Current.User.Identity.Name, userIsOnline);
221                 }
222                 
223                 public static MembershipUser GetUser (string username)
224                 {
225                         return GetUser (username, false);
226                 }
227                 
228                 public static MembershipUser GetUser (string username, bool userIsOnline)
229                 {
230                         return Provider.GetUser (username, userIsOnline);
231                 }
232                 
233                 public static MembershipUser GetUser (object providerUserKey)
234                 {
235                         return GetUser (providerUserKey, false);
236                 }
237                 
238                 public static MembershipUser GetUser (object providerUserKey, bool userIsOnline)
239                 {
240                         return Provider.GetUser (providerUserKey, userIsOnline);
241                 }
242                 
243                 public static string GetUserNameByEmail (string email)
244                 {
245                         return Provider.GetUserNameByEmail (email);
246                 }
247                 
248                 public static void UpdateUser (MembershipUser user)
249                 {
250                         Provider.UpdateUser (user);
251                 }
252                 
253                 public static bool ValidateUser (string username, string password)
254                 {
255                         return Provider.ValidateUser (username, password);
256                 }
257                 
258                 public static MembershipUserCollection FindUsersByEmail (string emailToMatch)
259                 {
260                         int totalRecords;
261                         return Provider.FindUsersByEmail (emailToMatch, 0, int.MaxValue, out totalRecords);
262                 }
263                 
264                 public static MembershipUserCollection FindUsersByEmail (string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
265                 {
266                         return Provider.FindUsersByEmail (emailToMatch, pageIndex, pageSize, out totalRecords);
267                 }
268                 
269                 public static MembershipUserCollection FindUsersByName (string nameToMatch)
270                 {
271                         int totalRecords;
272                         return Provider.FindUsersByName (nameToMatch, 0, int.MaxValue, out totalRecords);
273                 }
274                 
275                 public static MembershipUserCollection FindUsersByName (string nameToMatch, int pageIndex, int pageSize, out int totalRecords)
276                 {
277                         return Provider.FindUsersByName (nameToMatch, pageIndex, pageSize, out totalRecords);
278                 }
279
280                 public static string ApplicationName {
281                         get { return Provider.ApplicationName; }
282                         set { Provider.ApplicationName = value; }
283                 }
284                 
285                 public static bool EnablePasswordReset {
286                         get { return Provider.EnablePasswordReset; }
287                 }
288                 
289                 public static bool EnablePasswordRetrieval {
290                         get { return Provider.EnablePasswordRetrieval; }
291                 }
292                 
293                 public static bool RequiresQuestionAndAnswer {
294                         get { return Provider.RequiresQuestionAndAnswer; }
295                 }
296                 
297                 public static int MaxInvalidPasswordAttempts {
298                         get { return Provider.MaxInvalidPasswordAttempts; }
299                 }
300                 
301                 public static int MinRequiredNonAlphanumericCharacters {
302                         get { return Provider.MinRequiredNonAlphanumericCharacters; }
303                 }
304                 
305                 public static int MinRequiredPasswordLength {
306                         get { return Provider.MinRequiredPasswordLength; }
307                 }
308                 
309                 public static int PasswordAttemptWindow {
310                         get { return Provider.PasswordAttemptWindow; }
311                 }
312                 
313                 public static string PasswordStrengthRegularExpression {
314                         get { return Provider.PasswordStrengthRegularExpression; }
315                 }
316                                 
317                 public static MembershipProvider Provider {
318                         get { return provider; }
319                 }
320                 
321                 public static MembershipProviderCollection Providers {
322                         get { return providers; }
323                 }
324                 
325                 public static int UserIsOnlineTimeWindow {
326                         get { return onlineTimeWindow; }
327                 }
328                 
329                 public static event MembershipValidatePasswordEventHandler ValidatingPassword {
330                         add { Provider.ValidatingPassword += value; }
331                         remove { Provider.ValidatingPassword -= value; }
332                 }
333         }
334 }
335 #endif
336