How to create custom password validations with .Net Core Identity?
.Net Core Identity is an API that manages users, passwords, profile data and more. Many things are simple to do just calling properties, methods and assigning values. But what if we want to create password validations for specific cases?
For that we can use the ValidateAsync method that comes from the IPasswordValidator interface. The ValidateAsync method will need three parameters: userManager, user and password. This method will do the necessary checks when a new user is registered or a password is changed.
For example, if we want to verify that the user name is equal to the password at the time of registration, our code would look like this:

What happens is that IPasswordValidator will promote an abstraction of password validations. It will do some of the work for us. The only thing we need to do is code which check cases we want. In the case of the example above, we use the method parameters to check if the username is equal to the password, if he doesn’t fall into the condition, the registration ends successfully. Using the IdentityError class we can pass the error message we want.
From that we can add other checks like checking if the password has special characters from a specific list or if the password has repetitions, among other possibilities. Thus, the appropriate error messages will be displayed and the registration impediments will be performed like magic :) (.Net Core Identity abstractions).
If you know how to do this even better, let me know and let’s learn together. Bye! Tchau!