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

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.

Windows 8 Icons: Segoe UI Symbol

Whatz up guyz... In this post i am going to tell you about "Windows 8 Icons" . If you ever used a windows 8 app, you know that there are so many standard Icons. Actually we do not want to add images for each button, simply we can use the character map for obtain the icons.Go to the character map and select the font as "Segoe UI Symbol" .Then you will see lot of standard symbols in the character map.

SQLite Database for Windows 8 App : Adding existing SQLite DB file

Hi Guys ... This Post is about adding an existing SQLite database file to the Windows Metro App project. As I think "using SQLite" is the best way to create local database for Windows Rt applications.Actually SQLite is very easy to learn but I really think if Microsoft SQL Server CE supports windows rt applications it will more fun. .Isnt it ?. :)