Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Services.Authentication Prototype/Service/Provider/HeuristicLabMembershipProvider.cs @ 4024

Last change on this file since 4024 was 4024, checked in by bfarka, 14 years ago

last bugfixes for project #1046

File size: 25.6 KB
Line 
1using System;
2using System.Collections.Specialized;
3using System.Configuration;
4using System.Configuration.Provider;
5using System.IO;
6using System.Linq;
7using System.Security.Cryptography;
8using System.Text;
9using System.Web.Configuration;
10using System.Web.Security;
11using Persistence;
12
13
14namespace Service.Provider {
15  class HeuristicLabMembershipProvider : MembershipProvider {
16
17    #region variables
18
19    private string pApplicationName;
20    private bool pEnablePasswordReset;
21    private bool pEnablePasswordRetrieval;
22    private bool pRequiresQuestionAndAnswer;
23    private bool pRequiresUniqueEmail;
24    private int pMaxInvalidPasswordAttempts = 3;
25    private int pPasswordAttemptWindow;
26    private int pMinRequiredPasswordLength = 5;
27    private MembershipPasswordFormat pPasswordFormat = MembershipPasswordFormat.Clear;
28
29    #endregion
30
31    #region properties
32
33    public override string ApplicationName {
34      get { return pApplicationName; }
35      set { pApplicationName = value; }
36    }
37
38    public override bool EnablePasswordReset {
39      get { return false; }
40    }
41
42    public override bool EnablePasswordRetrieval {
43      get { return false; }
44    }
45
46    public override int MaxInvalidPasswordAttempts {
47      get { return pMaxInvalidPasswordAttempts; }
48    }
49
50    public override int MinRequiredNonAlphanumericCharacters {
51      get { return 0; }
52    }
53
54    public override int MinRequiredPasswordLength {
55      get { return pMinRequiredPasswordLength; }
56    }
57
58    public override int PasswordAttemptWindow {
59      get { return pPasswordAttemptWindow; }
60    }
61
62    public override MembershipPasswordFormat PasswordFormat {
63      get { return pPasswordFormat; }
64    }
65
66    public override string PasswordStrengthRegularExpression {
67      get { return string.Empty; }
68    }
69
70    public override bool RequiresQuestionAndAnswer {
71      get { return false; }
72    }
73
74    public override bool RequiresUniqueEmail {
75      get { return pRequiresUniqueEmail; }
76    }
77
78    #endregion
79
80    /// <summary>
81    /// this is the methode to initialize all important and needed
82    /// variables and settings
83    /// The most settings will be read from the web.config
84    /// </summary>
85    /// <param name="name"></param>
86    /// <param name="config"></param>
87    /// <returns></returns>
88
89    public override void Initialize(string name, NameValueCollection config) {
90      //
91      // Initialize values from web.config.
92      //
93
94      if (config == null)
95        throw new ArgumentNullException("config");
96
97      if (string.IsNullOrEmpty(name) || name.Length == 0)
98        name = "HeuristicLabMembershipProvider";
99
100      if (String.IsNullOrEmpty(config["description"])) {
101        config.Remove("description");
102        config.Add("description", "Heuristic Lab Membership provider");
103      }
104
105      // Initialize the abstract base class.
106      base.Initialize(name, config);
107
108      pApplicationName = GetConfigValue(config["applicationName"], System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
109      pMaxInvalidPasswordAttempts = Convert.ToInt32(GetConfigValue(config["maxInvalidPasswordAttempts"], "5"));
110      pPasswordAttemptWindow = Convert.ToInt32(GetConfigValue(config["passwordAttemptWindow"], "10"));
111      pMinRequiredPasswordLength = Convert.ToInt32(GetConfigValue(config["minRequiredPasswordLength"], "7"));
112      pEnablePasswordReset = Convert.ToBoolean(GetConfigValue(config["enablePasswordReset"], "true"));
113      pEnablePasswordRetrieval = Convert.ToBoolean(GetConfigValue(config["enablePasswordRetrieval"], "true"));
114      pRequiresQuestionAndAnswer = Convert.ToBoolean(GetConfigValue(config["requiresQuestionAndAnswer"], "false"));
115      pRequiresUniqueEmail = Convert.ToBoolean(GetConfigValue(config["requiresUniqueEmail"], "true"));
116
117      string tempFormat = config["passwordFormat"];
118      if (tempFormat == null) {
119        tempFormat = "Clear";
120      }
121
122      switch (tempFormat) {
123        case "Hashed":
124          pPasswordFormat = MembershipPasswordFormat.Hashed;
125          break;
126        case "Encrypted":
127          pPasswordFormat = MembershipPasswordFormat.Encrypted;
128          break;
129        case "Clear":
130          pPasswordFormat = MembershipPasswordFormat.Clear;
131          break;
132        default:
133          throw new ProviderException("Password format not supported.");
134      }
135    }
136
137
138    /// <summary>
139    /// this mehtode change the password of an existent user
140    /// the methode look for db connection and connect to
141    /// such db if it possible
142    /// Also neccessary requirements of password complexity is used
143    /// by this methode
144    /// </summary>
145    /// <param name="username"></param>
146    /// <param name="oldPassword"></param>
147    /// <param name="newPassword"></param>
148    /// <returns>
149    /// return true if password is changed, or false if it is not able
150    /// to set the password
151    /// </returns>
152    public override bool ChangePassword(string username, string oldPassword, string newPassword) {
153      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
154        // check database connection
155       
156        if (newPassword.Length < MinRequiredPasswordLength) {
157          return false;
158        }
159        if (db.HeuristicLabUsers.Count(x => x.UserName == username) > 0) {
160          HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
161
162          if (u.Password == EncodePassword(oldPassword) && newPassword.Length > 0) {
163            u.Password = EncodePassword(newPassword);
164            db.SubmitChanges();
165            return true;
166          }
167          return false;
168        } else {
169          return false;
170        }
171
172       
173      }
174    }
175
176    /// <summary>
177    /// change the password questionandanswer for specified user
178    ///
179    /// </summary>
180    /// <param name="username"></param>
181    /// <param name="password"></param>
182    /// <param name="newPasswordQuestion"></param>
183    /// <param name="newPasswordAnswer"></param>
184    /// <returns>
185    /// return true if changes are submitted
186    /// false if are no user found
187    /// </returns>
188    public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) {
189      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
190
191        if (db.HeuristicLabUsers.Count(x => x.UserName == username) > 0 && newPasswordQuestion.Length > 0 && newPasswordAnswer.Length > 0) {
192          HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
193          u.PasswordAnswer = newPasswordAnswer;
194          u.PasswordQuestion = newPasswordQuestion;
195
196          db.SubmitChanges();
197
198          return true;
199        }
200        return false;
201      }
202    }
203
204    /// <summary>
205    /// create a new user with username, password
206    /// email, passwordquestion, passwordAnswer,
207    /// Approvedstate, whether is providerUserKey and certain creation MembershipCreateStatus status
208    /// </summary>
209    /// <param name="username"></param>
210    /// <param name="password"></param>
211    /// <param name="email"></param>
212    /// <param name="passwordQuestion"></param>
213    /// <param name="passwordAnswer"></param>
214    /// <param name="providerUserKey"></param>
215    /// <param name="isApproved"></param>
216    /// <param name="status"></param>
217    /// <returns>
218    /// return a created user if there is no error occoured
219    /// return null if there is an error occoured
220    /// </returns>
221    public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) {
222      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
223        // check database connection
224        if (db == null) {
225          status = MembershipCreateStatus.ProviderError;
226          return null;
227        }
228        try {
229          // check for duplicate entries
230          if (db.HeuristicLabUsers.Count(x => x.UserName == username) > 0) {
231            status = MembershipCreateStatus.DuplicateUserName;
232            return null;
233          }
234          if (db.HeuristicLabUsers.Count(x => x.Email == email) > 0) {
235            status = MembershipCreateStatus.DuplicateEmail;
236            return null;
237          }
238          if (password.Length < MinRequiredPasswordLength) {
239            status = MembershipCreateStatus.InvalidPassword;
240            return null;
241          }
242
243          // create new user
244          HeuristicLabUser u = new HeuristicLabUser(username, email, passwordQuestion, "");
245          password = EncodePassword(password);
246          u.Password = password;
247          u.PasswordAnswer = passwordAnswer;
248          u.PasswordQuestion = passwordQuestion;
249          // save user into database
250          db.HeuristicLabUsers.InsertOnSubmit(u);
251          db.SubmitChanges();
252
253          // success
254          status = MembershipCreateStatus.Success;
255          return u.getMembershipUser(this.Name);
256        }
257        catch (Exception) {
258          // error
259          status = MembershipCreateStatus.ProviderError;
260          return null;
261        }
262      }
263    }
264
265
266    /// <summary>
267    /// delete a specified user from the membership data
268    /// </summary>
269    /// <param name="username"></param>
270    /// <param name="deleteAllRelatedData"></param>
271    /// <returns>
272    /// return a true if was possible to delete the user
273    /// return false if the db connection not exist or a exception occour
274    /// </returns>
275    public override bool DeleteUser(string username, bool deleteAllRelatedData) {
276      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
277        // check database connection
278        if (db == null) {
279          return false;
280        }
281        try {
282          // try to get user
283          HeuristicLabUser u =
284            db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == username);
285
286          // optionally delete related data
287          if (deleteAllRelatedData) {
288            db.HeuristicLabUserRole.DeleteAllOnSubmit<HeuristicLabUserRole>(u.HeuristicLabUserRoles);
289          }
290
291          // delete user
292          db.HeuristicLabUsers.DeleteOnSubmit(u);
293          db.SubmitChanges();
294          return true;
295        }
296        catch (Exception) {
297          return false;
298        }
299      }
300    }
301
302
303    /// <summary>
304    /// Returns a paged collection of users that match a given e-mail address
305    /// TODO: Return a sorted result set
306    /// </summary>
307    /// <param name="emailToMatch"></param>
308    /// <param name="pageIndex"></param>
309    /// <param name="pageSize"></param>
310    /// <param name="totalRecords"></param>
311    /// <returns></returns>
312    public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords) {
313      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
314        IQueryable<HeuristicLabUser> users = (from u in db.HeuristicLabUsers where u.Email == emailToMatch select u);
315        return PagedCollection(users, pageIndex, pageSize, out totalRecords);
316      }
317    }
318
319    /// <summary>
320    /// Returns a paged collection of users that match a given username
321    /// TODO: Return a sorted result set
322    /// </summary>
323    /// <param name="usernameToMatch"></param>
324    /// <param name="pageIndex"></param>
325    /// <param name="pageSize"></param>
326    /// <param name="totalRecords"></param>
327    /// <returns></returns>
328    public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords) {
329      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
330        IQueryable<HeuristicLabUser> users = (from u in db.HeuristicLabUsers where u.UserName == usernameToMatch select u);
331        return PagedCollection(users, pageIndex, pageSize, out totalRecords);
332      }
333
334    }
335
336    /// <summary>
337    /// Returns a paged collection of all users
338    /// TODO: Return a sorted result set
339    /// </summary>
340    /// <param name="pageIndex"></param>
341    /// <param name="pageSize"></param>
342    /// <param name="totalRecords"></param>
343    /// <returns></returns>
344    public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) {
345      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
346        // orderby u.UserName
347        // skipped ordering for now as the default comparator seems to be wrong
348        IQueryable<HeuristicLabUser> users = (from u in db.HeuristicLabUsers select u);
349        return PagedCollection(users, pageIndex, pageSize, out totalRecords);
350      }
351
352    }
353
354    /// <summary>
355    /// Helper method that takes an IQueriable object and returns a paged part of it
356    /// </summary>
357    /// <param name="querySource"></param>
358    /// <param name="pageIndex"></param>
359    /// <param name="pageSize"></param>
360    /// <param name="totalRecords"></param>
361    /// <returns></returns>
362    private MembershipUserCollection PagedCollection(IQueryable<HeuristicLabUser> querySource, int pageIndex, int pageSize, out int totalRecords) {
363      totalRecords = querySource.Count();
364      MembershipUserCollection userCollection = new MembershipUserCollection();
365      int skip = (pageIndex == 0) ? 0 : (pageIndex * pageSize);
366      foreach (HeuristicLabUser u in querySource.Skip(skip).Take(pageSize)) {
367        userCollection.Add(u.getMembershipUser(this.Name));
368      }
369      return userCollection;
370    }
371
372    /// <summary>
373    /// not jet implemented returns 0 as default
374    /// </summary>
375    /// <returns></returns>
376    public override int GetNumberOfUsersOnline() {
377      return 0;
378    }
379
380
381    /// <summary>
382    /// return the right password of the user
383    /// the user has do give the right answer to a question
384    /// to get his password information
385    /// </summary>
386    /// <param name="username"></param>
387    /// <param name="answer"></param>
388    /// <returns>
389    /// return a string type of the right password
390    /// </returns>
391    public override string GetPassword(string username, string answer) {
392      if (PasswordFormat == MembershipPasswordFormat.Hashed)
393        throw new NotSupportedException("Passwort is hashed");
394      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
395        HeuristicLabUser u =
396          db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == username);
397        if (EnablePasswordRetrieval && RequiresQuestionAndAnswer) {
398          if (u.PasswordAnswer.Equals(answer)) {
399            return u.Password;
400          } else {
401            throw new MembershipPasswordException();
402          }
403        } else throw new NotSupportedException("PasswortRetrival or RequiresQuestionAndAnswer not set");
404      }
405    }
406
407    /// <summary>
408    /// get a user of the type of the Membershipuser
409    /// </summary>
410    /// <param name="username"></param>
411    /// <param name="userisOnline"></param>
412    /// <returns>
413    /// returns the searched membershipuser
414    /// </returns>
415    public override MembershipUser GetUser(string username, bool userIsOnline) {
416      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
417        HeuristicLabUser u =
418            db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == username);
419        if (u != null)
420          return u.getMembershipUser(this.Name);
421        else
422          return null;
423      }
424    }
425
426
427    /// <summary>
428    /// search a user over the providerUserKey and return
429    /// the right membershipuser
430    /// </summary>
431    /// <param name="providerUserKey"></param>
432    /// <param name="userIsOnline"></param>
433    /// <returns>
434    /// return the right membershipuser
435    /// </returns>
436    public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) {
437      long ID = providerUserKey is long ? (long)providerUserKey : -1;
438      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
439        HeuristicLabUser u =
440            db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.ID == ID);
441        if (u != null)
442          return u.getMembershipUser(this.Name);
443        else
444          return null;
445      }
446    }
447
448    /// <summary>
449    /// search over the email the right username
450    /// </summary>
451    /// <param name="email"></param>
452    /// <returns>
453    /// return a string with the username
454    /// </returns>
455    public override string GetUserNameByEmail(string email) {
456      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
457
458        if (db.HeuristicLabUsers.Count(x => x.Email == email) > 0) {
459
460          HeuristicLabUser u =
461            db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.Email == email);
462
463          return u.UserName;
464        }
465        return null;
466      }
467    }
468
469    /// <summary>
470    /// after the question user have to give the right answer
471    /// then here will the password reseted
472    /// </summary>
473    /// <param name="username"></param>
474    /// <param name="answer"></param>
475    /// <returns>
476    /// return new password
477    /// </returns>
478    public override string ResetPassword(string username, string answer) {
479      throw new NotSupportedException("Restet password not avaliable");
480    }
481
482
483    /// <summary>
484    /// Lock a specified user over the username
485    /// </summary>
486    /// <param name="username"></param>
487    /// <returns>
488    /// return true if was possible and
489    /// flase if there was a exception
490    /// </returns>
491    public bool LockUser(string userName) {
492      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
493        // check database connection
494        if (db == null) {
495          return false;
496        }
497        try {
498          // try to get user
499          HeuristicLabUser u =
500            db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == userName);
501
502          // unlock user
503          u.Locked = true;
504          db.SubmitChanges();
505          return true;
506        }
507        catch (Exception) {
508          return false;
509        }
510      }
511    }
512
513
514    /// <summary>
515    /// unlock specified user over the username
516    /// </summary>
517    /// <param name="username"></param>
518    /// <returns>
519    /// return true if was possible and
520    /// false if not
521    /// </returns>
522    public override bool UnlockUser(string userName) {
523      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
524        // check database connection
525        if (db == null) {
526          return false;
527        }
528        try {
529          // try to get user
530          HeuristicLabUser u =
531            db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == userName);
532
533          // unlock user
534          u.Locked = false;
535          db.SubmitChanges();
536          return true;
537        }
538        catch (Exception) {
539          return false;
540        }
541      }
542    }
543
544
545    /// <summary>
546    /// Update existend user
547    /// </summary>
548    /// <param name="user"></param>
549    /// <returns>
550    /// </returns>
551    public override void UpdateUser(MembershipUser user) {
552      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
553        if (user != null && user.ProviderUserKey != null && db.HeuristicLabUsers.Count(x => x.ID == (long)user.ProviderUserKey) > 0) {
554          HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.ID == (long)user.ProviderUserKey);
555          u.UserName = user.UserName;
556          u.Comment = user.Comment;
557          u.Email = user.Email;
558          u.Locked = user.IsLockedOut;
559          db.SubmitChanges();
560
561        }
562      }
563    }
564    /// <summary>
565    /// Validates a user
566    /// </summary>
567    /// <param name="username"></param>
568    /// <param name="password"></param>
569    /// <returns></returns>
570    public override bool ValidateUser(string username, string password) {
571      bool isValid = false;
572      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
573        if (db == null) {
574          return false;
575        }
576        if (db.HeuristicLabUsers.Count(x => x.UserName == username && x.Locked == false) > 0) {
577          HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username && x.Locked == false);
578          isValid = CheckPassword(password, u.Password) && u.FailedLogins <= MaxInvalidPasswordAttempts;
579          if (!isValid) {
580            u.FailedLogins++;
581            if (u.FailedLogins > MaxInvalidPasswordAttempts) {
582              u.Locked = true;
583            }
584          } else {
585            u.FailedLogins = 0;
586          }
587
588          db.SubmitChanges();
589        }
590      }
591      return isValid;
592    }
593
594    /// <summary>
595    /// compaiers to passwords
596    /// </summary>
597    /// <param name="password"></param>
598    /// <param name="dbpassword"></param>
599    /// <returns></returns>
600    private bool CheckPassword(string password, string dbpassword) {
601      string pass1 = password;
602      string pass2 = dbpassword;
603
604      switch (PasswordFormat) {
605        case MembershipPasswordFormat.Encrypted:
606          pass2 = DecodePassword(dbpassword);
607          break;
608        case MembershipPasswordFormat.Hashed:
609          pass1 = EncodePassword(password);
610          break;
611        default:
612          break;
613      }
614
615      if (pass1 == pass2) {
616        return true;
617      }
618
619      return false;
620    }
621
622
623    /// <summary>
624    /// Encodes a password.
625    /// Public because needed for unit testing.
626    /// </summary>
627    /// <param name="password"></param>
628    /// <returns></returns>
629    public string EncodePassword(string password) {
630      string encodedPassword = password;
631
632      switch (PasswordFormat) {
633        case MembershipPasswordFormat.Clear:
634          break;
635        case MembershipPasswordFormat.Encrypted:
636          encodedPassword =
637            Convert.ToBase64String(EncryptPassword(Encoding.Unicode.GetBytes(password)));
638          break;
639        case MembershipPasswordFormat.Hashed:
640          SHA512 sha512 = SHA512.Create();
641          ASCIIEncoding encoder = new ASCIIEncoding();
642          byte[] combined = encoder.GetBytes(password);
643          sha512.ComputeHash(combined);
644          encodedPassword = Convert.ToBase64String(sha512.Hash);
645          break;
646        default:
647          throw new ProviderException("Unsupported password format.");
648      }
649
650      return encodedPassword;
651    }
652
653    private readonly byte[] _rgbKey = new byte[]
654                           {
655                             182, 140, 37, 101, 52, 157, 80, 17, 65, 35, 130, 208, 101, 68, 161, 45, 197, 102, 112, 190,
656                             187, 177, 37, 76, 63, 38, 190, 117, 247, 122, 94, 17
657                           };
658    private readonly byte[] _rgbIv = new byte[] { 60, 121, 178, 142, 50, 160, 226, 84, 41, 66, 158, 180, 26, 232, 42, 113 };
659
660    /// <summary>
661    /// encrypt passwort
662    /// </summary>
663    /// <param name="password"></param>
664    /// <returns>
665    /// return encrypted password
666    /// </returns>
667    protected override byte[] EncryptPassword(byte[] password) {
668      SymmetricAlgorithm sa = Aes.Create();
669      MemoryStream msEncrypt = new MemoryStream();
670      CryptoStream csEncrypt = new CryptoStream(msEncrypt, sa.CreateEncryptor(_rgbKey, _rgbIv), CryptoStreamMode.Write);
671      csEncrypt.Write(password, 0, password.Length);
672      csEncrypt.Close();
673      byte[] encryptedTextBytes = msEncrypt.ToArray();
674      msEncrypt.Close();
675      return encryptedTextBytes;
676    }
677    /// <summary>
678    /// decrypt passwort
679    /// </summary>
680    /// <param name="password"></param>
681    /// <returns>
682    /// return decrypted password
683    /// </returns>
684    protected override byte[] DecryptPassword(byte[] encodedPassword) {
685      SymmetricAlgorithm sa = Aes.Create();
686      MemoryStream msDecrypt = new MemoryStream(encodedPassword);
687      CryptoStream csDecrypt = new CryptoStream(msDecrypt, sa.CreateDecryptor(_rgbKey, _rgbIv), CryptoStreamMode.Read);
688      byte[] decryptedTextBytes = new Byte[encodedPassword.Length];
689      csDecrypt.Read(decryptedTextBytes, 0, encodedPassword.Length);
690      csDecrypt.Close();
691      msDecrypt.Close();
692      return decryptedTextBytes;
693    }
694
695    /// <summary>
696    /// Decodes a encoded Password
697    /// </summary>
698    /// <param name="encodedPassword"></param>
699    /// <returns></returns>
700    private string DecodePassword(string encodedPassword) {
701      string password = encodedPassword;
702
703      switch (PasswordFormat) {
704        case MembershipPasswordFormat.Clear:
705          break;
706        case MembershipPasswordFormat.Encrypted:
707          password =
708            Encoding.Unicode.GetString(DecryptPassword(Convert.FromBase64String(password))).TrimEnd('\0');
709
710
711          break;
712        case MembershipPasswordFormat.Hashed:
713          throw new ProviderException("Cannot unencode a hashed password.");
714        default:
715          throw new ProviderException("Unsupported password format.");
716      }
717
718      return password;
719    }
720
721    /// <summary>
722    /// returns the configuration string, if the value is null or empty the default value is returned
723    /// </summary>
724    /// <param name="configValue"></param>
725    /// <param name="defaultValue"></param>
726    /// <returns></returns>
727    private string GetConfigValue(string configValue, string defaultValue) {
728      if (String.IsNullOrEmpty(configValue))
729        return defaultValue;
730
731      return configValue;
732    }
733  }
734}
Note: See TracBrowser for help on using the repository browser.