Skip to main content

Password Validation: ASP.Net MVC 4

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 Createwith 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

Popular posts from this blog

Imagine Cup 2014

Hi guys .. Get ready for imagine cup 2014 In NewZealand .In the next few days imagine cup 2013 finals will happen in Russia. So It is time to wear your thinking hat.You have one year to develop your marvelous ideas.  The Microsoft Imagine Cup is the world’s most prestigious student technology competition, bringing together student innovators from all over the world. If you have a great idea for a new app, bring it to life through Imagine Cup. Over the past 10 years, more than 1.65 million students from 190 countries have participated in the Imagine Cup. 

Visual Studio 2013 New Editor Features

In Visual Studio 2013, we have introduced new features that boost productivity and save time when working inside the Editor. Some of these are new features and some are the most popular extensions from  Productivity Power Tools . These features are a result of the feedback you gave us through  User Voice  requests, forum posts and Connect bugs. The MVP community also helped us pick some of these experiences. Our primary focus for the Editor in this version is to keep the developer in context as much as possible. This blog post describes capabilities that bring information to your fingertips and allow you to do as much as possible without leaving your place in code.

C# Character Escape Sequences

Character combinations consisting of a backslash ( \ ) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences. An escape sequence is regarded as a single character and is therefore valid as a character constant.