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

Load an Addin into DraftSight

These days i am developing two plugins for Autocad 2012 &  DraftSight 2013.so when i was developing the plugin i got in to big trouble.Normally in the Autocad , we can load the .dll file simply typing the command "NETLOAD" but in the Draftsight i could not able to do that. It said  " D:\c \projects\c projects\DSAddinCSharp1\ DSAddinCSharp1\bin\Debug\ DSAddinCSharp1.dll is not a valid add-in "

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.

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.