Search code examples
asp.netasp.net-identity

Check if a password is valid using ASP.NET Identity 2


On my website, I give the administrators the possibility to change someone's password without entering the old one. I do the following:

userManager.RemovePassword(oldUser.Id);
userManager.AddPassword(oldUser.Id, newPassword);
         

However, this changes the password only if the newPassword string complies with the password policy set in the configuration files. AddPassword seems to fail silently when the new password does not fulfil the requirements.

Is there some simple way to check if a password is valid according to the current policy, apart from the obvious "manual procedure" (check how many upper/lowercase chars there are, how many digits, etc.). I'm looking for something like

bool valid = IsPasswordValid("pass");

Solution

  • You may be able to use the PasswordValidator.ValidateAsync() method to determine if a password meets the criteria defined in your UserManager :

    var valid = (await UserManager.PasswordValidator.ValidateAsync("pass")).Succeeded;