Implement dynamic Password Policy from database using RegularExpression Validator in ASP.Net

Last Reply on Jun 02, 2014 04:36 AM By Azim

Posted on Jun 02, 2014 04:12 AM

I had implemented Password Policy in which user should enter atleast 8 character of password with below complexity while setting password:

1 capital/Uppercase character , 1 special character and 1 number

I tried below code for above:

<asp:TextBox ID="passwordtxt" runat="server" Width="100%" TextMode="Password" MaxLength="10"></asp:TextBox>
<asp:RegularExpressionValidator ID="Regex1" runat="server" ControlToValidate="passwordtxt" ValidationExpression="^(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$" ErrorMessage="Password must contain: Minimum 8 characters with atleast 1 Uppercase letter, 1 special character and 1 Number" ForeColor="Red" />

by referring below article:

http://www.aspsnippets.com/Articles/Implement-Password-Policy-using-Regular-Expressions-and-ASPNet-RegularExpression-Validator.aspx

But in this code, I have to follow that:

while entering password if it is below 8 character, then msg should come "password should be atleast 8 characters"
and after entering password, when user tries to save password, and if password is missing any of the complexity i.e., 1 capital/Uppercase character , 1 special character and 1 number,

then msg should come "password should have 1 capital/Uppercase character , 1 special character and 1 number".

Also, this complexity feature should be configurable in Database table.
If enabled, then enter complex password
If disabled, then only 8 charatcter password with no cpmlexity

 

Please reply how to achieve that.

Posted on Jun 02, 2014 04:36 AM

You can bring the Password policy is Active ot not from Database. But here i am switching the Regular Expression Validator using DropDownList.

HTML

<form id="form1" runat="server">
<asp:DropDownList ID="ddlPasswordPolicy" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList_OnSelectedIndexChanged">
    <asp:ListItem Text="Please Select" Value="-1" />
    <asp:ListItem Text="Only 8 Characters" Value="0" />
    <asp:ListItem Text="With Password Policy" Value="1" />
</asp:DropDownList>
<div>
    <u>Minimum 8 characters atleast 1 UpperCase Alphabet, 1 LowerCase Alphabet, 1 Number
        and 1 Special Character</u> <b>OR</b> <u>Minimum 8 characters atleast Character</u>
    <br />
    Example: PaSs@123 OR pAss@123
    <br />
    <br />
    <asp:TextBox ID="txtPolicy4" runat="server"></asp:TextBox><br />
    <asp:RegularExpressionValidator ID="Regex4" runat="server" ControlToValidate="txtPolicy4"
        Display="Dynamic" ValidationExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}"
        ErrorMessage="Password must contain: Minimum 8 characters atleast 1 UpperCase Alphabet, 1 LowerCase Alphabet, 1 Number and 1 Special Character"
        ForeColor="Red" />
    <asp:RegularExpressionValidator ID="Regex" runat="server" ValidationExpression="\S{8,8}"
        ControlToValidate="txtPolicy4" Display="Dynamic" ErrorMessage="Password must contain: Minimum 8 characters Character"
        ForeColor="Red" />
</div>
</form>

C#

protected void DropDownList_OnSelectedIndexChanged(object sender, EventArgs e)
{
    if (this.ddlPasswordPolicy.SelectedItem.Value == "0")
    {
        this.Regex4.Enabled = false;
    }
    else
    {
        this.Regex4.Enabled = true;
    }
}

 

I agree, here is the link: http://e-iceblue.com/free-api