Click here to Skip to main content
15,888,521 members
Articles / Web Development / ASP.NET
Tip/Trick

Customizable Password Policy C#

Rate me:
Please Sign up or sign in to vote.
4.25/5 (3 votes)
7 Jul 2011CPOL 37.9K   3   6
Introduction
To enforce password strength in such a way that user can configure the number of uppercase, lowercase, special characters and digits the password can contain.

Background
One of my projects, there arises a scenario in which the password strength is configure in the database. Have to make use of this database configuration to validate the password entered by the user. So I needed a class which can enforce this and should be highly customizable.

Using the code
Password Policy contains a method IsValid which takes password string as its parameter and checks for various conditions like minimum lenth of the password, the number of uppercase or lowercase the password can contain. The user can also customize the no of digits and non-alpha numeric characters also.
It does all the Counts throug the Regex.Matches function which retuns the number of occurances of the pattern.

public class PasswordPolicy
   {
       private static int Minimum_Length = 7;
       private static int Upper_Case_length = 1;
       private static int Lower_Case_length = 1;
       private static int NonAlpha_length = 1;
       private static int Numeric_length = 1;

       public static bool IsValid(string Password)
       {
           if (Password.Length < Minimum_Length)
                return false;
            if (UpperCaseCount(Password) < Upper_Case_length)
                return false;
            if (LowerCaseCount(Password) < Lower_Case_length)
                return false;
            if (NumericCount(Password) < 1)
                return false;
            if (NonAlphaCount(Password) < NonAlpha_length)
                return false;
            return true;
        }

       private static int UpperCaseCount(string Password)
       {
           return Regex.Matches(Password, "[A-Z]").Count;
       }

       private static int LowerCaseCount(string Password)
       {
           return Regex.Matches(Password, "[a-z]").Count;
       }
       private static int NumericCount(string Password)
       {
           return Regex.Matches(Password, "[0-9]").Count;
       }
       private static int NonAlphaCount(string Password)
       {
           return Regex.Matches(Password, @"[^0-9a-zA-Z\._]").Count;
       }
   }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Tikona Digital Networks Pvt. LTD
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Er. Abhinav Kumar Singh26-Aug-13 20:01
professionalEr. Abhinav Kumar Singh26-Aug-13 20:01 
Greate work sir
GeneralReason for my vote of 3 Less of an 'article' and more of a '... Pin
tlhIn`toq12-Jul-11 6:42
tlhIn`toq12-Jul-11 6:42 
GeneralHi Mohamed, Thanks for your comments. will update the tip so... Pin
Srinivasan Raj12-Jul-11 1:19
Srinivasan Raj12-Jul-11 1:19 
GeneralI am really happy about your subject. on other hand, to use ... Pin
Mohamed Karfaa11-Jul-11 1:54
Mohamed Karfaa11-Jul-11 1:54 
GeneralRe: Hi Mohamed, Thanks for your comments. will update the tip so... Pin
Srinivasan Raj12-Jul-11 1:19
Srinivasan Raj12-Jul-11 1:19 
SuggestionMy vote of 4 Pin
Reiss8-Jul-11 1:14
professionalReiss8-Jul-11 1:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.