Password Validation: ASP.Net MVC 4
Wsup Guys…
Let’s have a small talk about how to customize password validation
in ASP.Net MVC default template.
There are 5 password validations
in ASP.Net MVC default template. Those are
- · The Password must be at least 6 characters long.
- · Passwords must have at least one non letter character.
- · Passwords must have at least one digit character.
- · Passwords must have at least one lowercase ('a'-'z').
- · Passwords must have at least one uppercase ('A'-'Z').
Let’s see how we can add or remove password validations.
Open Visual Studio and Create
MVC 4 Web Application Project.
NOTE:
You can run
the created web application and check currently available validation for
password in default ASP.Net MVC Web application. To da that Click on the
register and try to register a user as below
1.
Password
only contain less than 6 characters
2.
Password
not contains any letter character
3.
Password
not contains any digit character
4.
Password
not contains any lowercase letter
5.
Password
not contains any uppercase letter
You will
get following errors.
How to Customize Password Validation
1. Open
IdentityConfig.cs file which is inside App_Start folder.
2. Go to ApplicationUserManager class which is
inherited from UserManager
public class ApplicationUserManager : UserManager<ApplicationUser>
{
}
3. Inside that class, there will be a method name “Create” with return type of
ApplicationUserManager
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
}
This where you can add or remove
password valid`ations
// Configure validation logic for
passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
You can change password
validations in here and check how it works.
As an example, if you don’t want
any uppercase character in your password then make
RequireUppercase as false.
RequireUppercase = false,
NOTE:
If you want
to change the password minimum length, that would be little bit tricky. You
have to go to RegisterViewModel class
in AccountViewModels.cs file and change the password StringLength,
MinimumLength field.
[StringLength(100, ErrorMessage = "The {0} must be at least {2}
characters long.", MinimumLength = 6)]
More About PasswordValidator Class
Properties and Methods
Reference
T H A N K Y O U
Comments