Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomPasswordValidator : PasswordValidator<AppUser>
- { //override the PasswordValidator functionality with the custom definitions
- public override async Task<IdentityResult> ValidateAsync(UserManager<AppUser> manager, AppUser user, string password)
- {
- IdentityResult result = await base.ValidateAsync(manager, user, password);
- List<IdentityError> errors = result.Succeeded ? new List<IdentityError>() : result.Errors.ToList();
- //check that the username is not in the password
- if (password.ToLower().Contains(user.UserName.ToLower()))
- {
- errors.Add(new IdentityError
- {
- Code = "PasswordContainsUserName",
- Description = "Password cannot contain username"
- });
- }
- //check that the password doesn't contain '12345'
- if (password.Contains("12345"))
- {
- errors.Add(new IdentityError
- {
- Code = "PasswordContainsSequence",
- Description = "Password cannot contain numeric sequence"
- });
- }
- //return Task.FromResult(errors.Count == 0 ? IdentityResult.Success : IdentityResult.Failed(errors.ToArray()));
- return errors.Count == 0 ? IdentityResult.Success : IdentityResult.Failed(errors.ToArray());
- }
- }
- //test the ability to validate new passwords with Infrastructure/CustomPasswordValidator.cs
- [Fact]
- public void Validate_Password()
- {
- //Arrange
- Mock<UserManager<AppUser>> userManager = new Mock<UserManager<AppUser>>();
- Mock<SignInManager<AppUser>> signInManager = new Mock<SignInManager<AppUser>>();
- //private UserManager<AppUser> userManager;
- Mock<AppUser> user = new Mock<AppUser>();
- //How can I assign user name/password values here?
- user.Name = "user";
- //create a password that has the username and sequence '12345' to generate errors
- user.Password = "user12345";
- //Act
- //I get an error here:
- IdentityResult result = PasswordValidator<AppUser>.ValidateAsync(userManager, user, user.Password);
- //Assert
- //demonstrate that there are two errors present
- List<IdentityError> errors = result.Succeeded ? new List<IdentityError>() : result.Errors.ToList();
- Assert.Equal(errors.Count, 2);
- }
Add Comment
Please, Sign In to add comment