public void SetPassword(string password) { if (string.IsNullOrEmpty(password)) { throw new ArgumentException("Password cannot be empty"); } if (PasswordComplexityRegex != null && !PasswordComplexityRegex.IsMatch(password)) { throw new ArgumentException("Password does not meet complexity requirements"); } if (NumberOfPreviousPasswordsToStore.HasValue && this.PreviousPasswordHashes.Take(NumberOfPreviousPasswordsToStore.Value).Any(p => CryptoHelper.VerifyHash(password, p))) { throw new ArgumentException("Password cannot be the same as any of the previous " + NumberOfPreviousPasswordsToStore.ToString()); } this.PasswordHash = CryptoHelper.ComputeHash(password); this.PreviousPasswordHashes = Enumerable.Repeat(this.PasswordHash, 1).Concat(this.PreviousPasswordHashes).Take(NumberOfPreviousPasswordsToStore ?? 0).ToArray(); this.LastPasswordChangedDate = DateTime.UtcNow; this.PasswordExpiryDate = NumberOfDaysUntilPasswordChangeRequired.HasValue ? this.LastPasswordChangedDate.AddDays(NumberOfDaysUntilPasswordChangeRequired.Value) : (DateTime?)null; }
public IEnumerable <ValidationResult> ValidateNewPassword(string password, string memberName) { if (string.IsNullOrEmpty(password)) { yield return(new ValidationResult("Password cannot be empty", Enumerable.Repeat(memberName, 1))); } else if (PasswordComplexityRegex != null && !PasswordComplexityRegex.IsMatch(password)) { yield return(new ValidationResult("Password does not meet complexity requirements", Enumerable.Repeat(memberName, 1))); } else if (NumberOfPreviousPasswordsToStore.HasValue && this.PreviousPasswordHashes.Take(NumberOfPreviousPasswordsToStore.Value).Any(p => CryptoHelper.VerifyHash(password, p))) { yield return(new ValidationResult("Password cannot be the same as any of the previous " + NumberOfPreviousPasswordsToStore.ToString(), Enumerable.Repeat(memberName, 1))); } yield break; }