Click here to Skip to main content
Click here to Skip to main content

Tagged as

Customizable Password Policy C#

, 7 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Srinivasan Raj
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMember 1023406826-Aug-13 21:01 
GeneralReason for my vote of 3 Less of an 'article' and more of a '... Pinmembertlhintoq12-Jul-11 7:42 
GeneralHi Mohamed, Thanks for your comments. will update the tip so... PinmemberSrinivasan Raj12-Jul-11 2:19 
GeneralI am really happy about your subject. on other hand, to use ... PinmemberMohamed Karfaa11-Jul-11 2:54 
GeneralRe: Hi Mohamed, Thanks for your comments. will update the tip so... PinmemberSrinivasan Raj12-Jul-11 2:19 
SuggestionMy vote of 4 PinmemberReiss8-Jul-11 2:14 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411019.1 | Last Updated 7 Jul 2011
Article Copyright 2011 by Srinivasan Raj
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid