Guest User

Untitled

a guest
Dec 26th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. public class CustomPasswordValidator : PasswordValidator<AppUser>
  2. { //override the PasswordValidator functionality with the custom definitions
  3. public override async Task<IdentityResult> ValidateAsync(UserManager<AppUser> manager, AppUser user, string password)
  4. {
  5. IdentityResult result = await base.ValidateAsync(manager, user, password);
  6.  
  7. List<IdentityError> errors = result.Succeeded ? new List<IdentityError>() : result.Errors.ToList();
  8.  
  9. //check that the username is not in the password
  10. if (password.ToLower().Contains(user.UserName.ToLower()))
  11. {
  12. errors.Add(new IdentityError
  13. {
  14. Code = "PasswordContainsUserName",
  15. Description = "Password cannot contain username"
  16. });
  17. }
  18.  
  19. //check that the password doesn't contain '12345'
  20. if (password.Contains("12345"))
  21. {
  22. errors.Add(new IdentityError
  23. {
  24. Code = "PasswordContainsSequence",
  25. Description = "Password cannot contain numeric sequence"
  26. });
  27. }
  28. //return Task.FromResult(errors.Count == 0 ? IdentityResult.Success : IdentityResult.Failed(errors.ToArray()));
  29. return errors.Count == 0 ? IdentityResult.Success : IdentityResult.Failed(errors.ToArray());
  30. }
  31. }
  32.  
  33. //test the ability to validate new passwords with Infrastructure/CustomPasswordValidator.cs
  34. [Fact]
  35. public void Validate_Password()
  36. {
  37. //Arrange
  38. Mock<UserManager<AppUser>> userManager = new Mock<UserManager<AppUser>>();
  39. Mock<SignInManager<AppUser>> signInManager = new Mock<SignInManager<AppUser>>();
  40. //private UserManager<AppUser> userManager;
  41.  
  42. Mock<AppUser> user = new Mock<AppUser>();
  43. //How can I assign user name/password values here?
  44. user.Name = "user";
  45. //create a password that has the username and sequence '12345' to generate errors
  46. user.Password = "user12345";
  47.  
  48. //Act
  49. //I get an error here:
  50. IdentityResult result = PasswordValidator<AppUser>.ValidateAsync(userManager, user, user.Password);
  51.  
  52. //Assert
  53. //demonstrate that there are two errors present
  54. List<IdentityError> errors = result.Succeeded ? new List<IdentityError>() : result.Errors.ToList();
  55. Assert.Equal(errors.Count, 2);
  56. }
Add Comment
Please, Sign In to add comment