/
WCMembershipProvider.cs
564 lines (552 loc) · 20.8 KB
/
WCMembershipProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.Security;
using System.Text.RegularExpressions;
using System.Configuration;
namespace Windchime
{
public class WCMembershipProvider : System.Web.Security.MembershipProvider
{
public static bool isEmail(string inputEmail)
{
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex re = new Regex(strRegex);
if (re.IsMatch(inputEmail))
return (true);
else
return (false);
}
public override string ApplicationName { get { return "Windchime"; } set { ; } }
//
// Summary:
// Indicates whether the membership provider is configured to allow users to
// reset their passwords.
//
// Returns:
// true if the membership provider supports password reset; otherwise, false.
// The default is true.
public override bool EnablePasswordReset { get { return false; } }
//
// Summary:
// Indicates whether the membership provider is configured to allow users to
// retrieve their passwords.
//
// Returns:
// true if the membership provider is configured to support password retrieval;
// otherwise, false. The default is false.
public override bool EnablePasswordRetrieval { get { return false; } }
//
// Summary:
// Gets the number of invalid password or password-answer attempts allowed before
// the membership user is locked out.
//
// Returns:
// The number of invalid password or password-answer attempts allowed before
// the membership user is locked out.
public override int MaxInvalidPasswordAttempts { get { return 9999; } }
//
// Summary:
// Gets the minimum number of special characters that must be present in a valid
// password.
//
// Returns:
// The minimum number of special characters that must be present in a valid
// password.
public override int MinRequiredNonAlphanumericCharacters { get { return 1; } }
//
// Summary:
// Gets the minimum length required for a password.
//
// Returns:
// The minimum length required for a password.
public override int MinRequiredPasswordLength { get { return 8; } }
//
// Summary:
// Gets the number of minutes in which a maximum number of invalid password
// or password-answer attempts are allowed before the membership user is locked
// out.
//
// Returns:
// The number of minutes in which a maximum number of invalid password or password-answer
// attempts are allowed before the membership user is locked out.
public override int PasswordAttemptWindow { get { return 9999; } }
//
// Summary:
// Gets a value indicating the format for storing passwords in the membership
// data store.
//
// Returns:
// One of the System.Web.Security.MembershipPasswordFormat values indicating
// the format for storing passwords in the data store.
public override MembershipPasswordFormat PasswordFormat { get { return MembershipPasswordFormat.Hashed; } }
//
// Summary:
// Gets the regular expression used to evaluate a password.
//
// Returns:
// A regular expression used to evaluate a password.
public override string PasswordStrengthRegularExpression
{
get
{
//at least 1 alphanum, at least 1 special, 8-20 characters all together
return "^(?=.*[a-zA-Z]+.*)(?=.*[!@#$%^&*]+.*)[0-9a-zA-Z!@#$%^&*]{" + MinRequiredPasswordLength.ToString() + ",}$";
}
}
//
// Summary:
// Gets a value indicating whether the membership provider is configured to
// require the user to answer a password question for password reset and retrieval.
//
// Returns:
// true if a password answer is required for password reset and retrieval; otherwise,
// false. The default is true.
public override bool RequiresQuestionAndAnswer { get { return false; } }
//
// Summary:
// Gets a value indicating whether the membership provider is configured to
// require a unique e-mail address for each user name.
//
// Returns:
// true if the membership provider requires a unique e-mail address; otherwise,
// false. The default is true.
public override bool RequiresUniqueEmail { get { return true; } }
// Summary:
// Occurs when a user is created, a password is changed, or a password is reset.
//public event MembershipValidatePasswordEventHandler ValidatingPassword;
// Summary:
// Processes a request to update the password for a membership user.
//
// Parameters:
// username:
// The user to update the password for.
//
// oldPassword:
// The current password for the specified user.
//
// newPassword:
// The new password for the specified user.
//
// Returns:
// true if the password was updated successfully; otherwise, false.
public override bool ChangePassword(string username, string oldPassword, string newPassword)
{
return false;
}
//
// Summary:
// Processes a request to update the password question and answer for a membership
// user.
//
// Parameters:
// username:
// The user to change the password question and answer for.
//
// password:
// The password for the specified user.
//
// newPasswordQuestion:
// The new password question for the specified user.
//
// newPasswordAnswer:
// The new password answer for the specified user.
//
// Returns:
// true if the password question and answer are updated successfully; otherwise,
// false.
public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
{
return false;
}
//
// Summary:
// Adds a new membership user to the data source.
//
// Parameters:
// username:
// The user name for the new user.
//
// password:
// The password for the new user.
//
// email:
// The e-mail address for the new user.
//
// passwordQuestion:
// The password question for the new user.
//
// passwordAnswer:
// The password answer for the new user
//
// isApproved:
// Whether or not the new user is approved to be validated.
//
// providerUserKey:
// The unique identifier from the membership data source for the user.
//
// status:
// A System.Web.Security.MembershipCreateStatus enumeration value indicating
// whether the user was created successfully.
//
// Returns:
// A System.Web.Security.MembershipUser object populated with the information
// for the newly created user.
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
using (WindchimeEntities wce = new WindchimeEntities())
{
Regex re = new Regex(this.PasswordStrengthRegularExpression);
User u = new User();
Group g = new Group();
u.FirstName = "";
u.LastName = "";
u.Username = username;
u.Password = SecurityManager.HashPasswordForStoringInDatabase(password);
u.IsStaff = false;
u.Email = email;
g.Name = username;
g.IsSpecial = false;
if (username.Length < 6)
{
status = MembershipCreateStatus.UserRejected;
}
else if ((from User k in wce.CreatorSet.OfType<User>()
where k.Username == username
select k).Count<User>() > 0)
{
status = MembershipCreateStatus.DuplicateUserName;
}
else if (!re.IsMatch(password))
{
status = MembershipCreateStatus.InvalidPassword;
}
else if (!isEmail(email))
{
status = MembershipCreateStatus.InvalidEmail;
}
else if ((from User k in wce.CreatorSet.OfType<User>()
where k.Email == email
select k).Count<User>() > 0)
{
status = MembershipCreateStatus.DuplicateEmail;
}
else
{
status = MembershipCreateStatus.Success;
wce.AddToCreatorSet(u);
wce.AddToGroups(g);
g.Users.Add(u);
wce.SaveChanges();
// log in the user
WindchimeSession.Current.User = u;
}
}
return null;
}
//
// Summary:
// Decrypts an encrypted password.
//
// Parameters:
// encodedPassword:
// A byte array that contains the encrypted password to decrypt.
//
// Returns:
// A byte array that contains the decrypted password.
//
// Exceptions:
// System.Configuration.Provider.ProviderException:
// The System.Web.Configuration.MachineKeySection.ValidationKey property or
// System.Web.Configuration.MachineKeySection.DecryptionKey property is set
// to AutoGenerate.
/*protected virtual byte[] DecryptPassword(byte[] encodedPassword)
{
return null;
}*/
//
// Summary:
// Removes a user from the membership data source.
//
// Parameters:
// username:
// The name of the user to delete.
//
// deleteAllRelatedData:
// true to delete data related to the user from the database; false to leave
// data related to the user in the database.
//
// Returns:
// true if the user was successfully deleted; otherwise, false.
public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
return false;
}
//
// Summary:
// Encrypts a password.
//
// Parameters:
// password:
// A byte array that contains the password to encrypt.
//
// Returns:
// A byte array that contains the encrypted password.
//
// Exceptions:
// System.Configuration.Provider.ProviderException:
// The System.Web.Configuration.MachineKeySection.ValidationKey property or
// System.Web.Configuration.MachineKeySection.DecryptionKey property is set
// to AutoGenerate.
/*protected virtual byte[] EncryptPassword(byte[] password)
{
return null;
}*/
//
// Summary:
// Gets a collection of membership users where the e-mail address contains the
// specified e-mail address to match.
//
// Parameters:
// emailToMatch:
// The e-mail address to search for.
//
// pageIndex:
// The index of the page of results to return. pageIndex is zero-based.
//
// pageSize:
// The size of the page of results to return.
//
// totalRecords:
// The total number of matched users.
//
// Returns:
// A System.Web.Security.MembershipUserCollection collection that contains a
// page of pageSizeSystem.Web.Security.MembershipUser objects beginning at the
// page specified by pageIndex.
public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
{
totalRecords = 0;
return null;
}
//
// Summary:
// Gets a collection of membership users where the user name contains the specified
// user name to match.
//
// Parameters:
// usernameToMatch:
// The user name to search for.
//
// pageIndex:
// The index of the page of results to return. pageIndex is zero-based.
//
// pageSize:
// The size of the page of results to return.
//
// totalRecords:
// The total number of matched users.
//
// Returns:
// A System.Web.Security.MembershipUserCollection collection that contains a
// page of pageSizeSystem.Web.Security.MembershipUser objects beginning at the
// page specified by pageIndex.
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{
totalRecords = 0;
return null;
}
//
// Summary:
// Gets a collection of all the users in the data source in pages of data.
//
// Parameters:
// pageIndex:
// The index of the page of results to return. pageIndex is zero-based.
//
// pageSize:
// The size of the page of results to return.
//
// totalRecords:
// The total number of matched users.
//
// Returns:
// A System.Web.Security.MembershipUserCollection collection that contains a
// page of pageSizeSystem.Web.Security.MembershipUser objects beginning at the
// page specified by pageIndex.
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
{
totalRecords = 0;
return null;
}
//
// Summary:
// Gets the number of users currently accessing the application.
//
// Returns:
// The number of users currently accessing the application.
public override int GetNumberOfUsersOnline()
{
return 0;
}
//
// Summary:
// Gets the password for the specified user name from the data source.
//
// Parameters:
// username:
// The user to retrieve the password for.
//
// answer:
// The password answer for the user.
//
// Returns:
// The password for the specified user name.
public override string GetPassword(string username, string answer)
{
return "";
}
//
// Summary:
// Gets user information from the data source based on the unique identifier
// for the membership user. Provides an option to update the last-activity date/time
// stamp for the user.
//
// Parameters:
// providerUserKey:
// The unique identifier for the membership user to get information for.
//
// userIsOnline:
// true to update the last-activity date/time stamp for the user; false to return
// user information without updating the last-activity date/time stamp for the
// user.
//
// Returns:
// A System.Web.Security.MembershipUser object populated with the specified
// user's information from the data source.
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
return null;
}
//
// Summary:
// Gets information from the data source for a user. Provides an option to update
// the last-activity date/time stamp for the user.
//
// Parameters:
// username:
// The name of the user to get information for.
//
// userIsOnline:
// true to update the last-activity date/time stamp for the user; false to return
// user information without updating the last-activity date/time stamp for the
// user.
//
// Returns:
// A System.Web.Security.MembershipUser object populated with the specified
// user's information from the data source.
public override MembershipUser GetUser(string username, bool userIsOnline)
{
return null;
}
//
// Summary:
// Gets the user name associated with the specified e-mail address.
//
// Parameters:
// email:
// The e-mail address to search for.
//
// Returns:
// The user name associated with the specified e-mail address. If no match is
// found, return null.
public override string GetUserNameByEmail(string email)
{
return null;
}
//
// Summary:
// Raises the System.Web.Security.MembershipProvider.ValidatingPassword event
// if an event handler has been defined.
//
// Parameters:
// e:
// The System.Web.Security.ValidatePasswordEventArgs to pass to the System.Web.Security.MembershipProvider.ValidatingPassword
// event handler.
/*protected override virtual void OnValidatingPassword(ValidatePasswordEventArgs e)
{
}*/
//
// Summary:
// Resets a user's password to a new, automatically generated password.
//
// Parameters:
// username:
// The user to reset the password for.
//
// answer:
// The password answer for the specified user.
//
// Returns:
// The new password for the specified user.
public override string ResetPassword(string username, string answer)
{
return "";
}
//
// Summary:
// Clears a lock so that the membership user can be validated.
//
// Parameters:
// userName:
// The membership user whose lock status you want to clear.
//
// Returns:
// true if the membership user was successfully unlocked; otherwise, false.
public override bool UnlockUser(string userName)
{
return false;
}
//
// Summary:
// Updates information about a user in the data source.
//
// Parameters:
// user:
// A System.Web.Security.MembershipUser object that represents the user to update
// and the updated information for the user.
public override void UpdateUser(MembershipUser user)
{
}
public override bool ValidateUser(string strName, string strPassword)
{
using (WindchimeEntities wce = new WindchimeEntities())
{
string pw = SecurityManager.HashPasswordForStoringInDatabase(strPassword);
var users = (from User u in wce.CreatorSet.OfType<User>()
where u.Username == strName && u.Password == pw
select u);
int num = users.Count<User>();
if (num > 1)
{
//throw new MultipleUsersException(); //doesn't exist right now
throw new Exception("Multiple users in system with same credentials!");
}
else if (num == 0)
{
return false;
}
else
{
WindchimeSession.Current.User = users.First<User>();
return true;
}
}
}
}
}