PasswordStrength in ASP.NET AJAX Control Toolkit

ASP.NET AJAX control toolkit is a great set of helpful controls for ASP.NET AJAX. Usually many of these controls aren't covered in books and articles most likely because their usage is simple.

However, yesterday I was playing with PasswordStrength control included in ASP.NET AJAX control toolkit to use it in a project and thought it would be good to write a descriptive post about it since there is only one resource about this control which is its sample on ASP.NET site.

PasswordStrength is a simple control that shows the strength of the passwords that users enter in TextBoxes in a visual manner using ASP.NET AJAX. This control is customizable to show the strength in several ways.

All you need to use this control is a ScriptManager control on your page. After that, you can insert this control somewhere in your page. Obviously you need to use this control with TextBox controls that have their TextMode property set to Password.

There are two ways to add this control to your pages. One is adding the text element of this control to your page source and setting its TargetControlID property to the name of the TextBox that you want this control to validate it (or same thing in programming code). The other way which is easier is switching to design mode and drag and drop this control into the TextBox . This automatically generates necessary code for you.

This control has several properties that customize its behavior but many of them are optional and control uses their default values. The only property that you need to take care about is that TargetControlID which is required and must be set to the name of your TextBox control.

For this post I'm going to use an example where I create a registration page for a sample site that gets user's email as well as his or her password and its confirmation. I exclude some obvious stuff like validation and code logic for simplicity.

Here I want to use two PasswordStrength controls to check the strength of the entered passwords. In the simplest form my code would look like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"

 Inherits="PasswordStrengthSample._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>PasswordStrength Sample</title>

</head>

<body>

    <form id="form1" runat="server">

    <ajaxControlToolkit:ToolkitScriptManager runat="server">

    </ajaxControlToolkit:ToolkitScriptManager>

    <h3>User Reigstration</h3>

    Email:<br />

    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />

    Password:<br />

    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>

    <ajaxControlToolkit:PasswordStrength ID="txtPassword_PasswordStrength" runat="server"

        TargetControlID="txtPassword">

    </ajaxControlToolkit:PasswordStrength>

    <br />

    Confirm Password:<br />

    <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>

    <ajaxControlToolkit:PasswordStrength ID="txtConfirmPassword_PasswordStrength" runat="server"

        TargetControlID="txtConfirmPassword">

    </ajaxControlToolkit:PasswordStrength>

    <br />

    <asp:Button ID="btnRegister" runat="server" Text="Register" />

    <div>

    </div>

    </form>

</body>

</html>

This provides a result like something that you see here:

Output

This is the simplest form with default settings. But PasswordStrength is customizable via a set of properties. Here is a list and short description of its common properties. The full description is available here. Note that not all of these properties are very common and important.

Having this background, let me customize this control in two ways. First I customize the first PasswordStrength control with text indicator and then customize the second one with the bar indicator.

For the first control I set some properties to control the length of the password and the minimum number of different characters in it. I also set its DisplayPosition property to show the indicator in the below right of the TextBox. The other property is PrefixText that I set it to "Your password is". I also set TextStrengthDescirptions property to a list of three values ("Weak", "Not Good" and "Good") and write three CSS classes with different background colors to set them in TextStrengthDescriptionStyles.

Updated version of my code is presented here.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"

 Inherits="PasswordStrengthSample._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>PasswordStrength Sample</title>

    <style>

    .TextStrengthWeak

    {

        background: red;

    }

    .TextStrengthNotGood

    {

        background: yellow;

    }

    .TextStrengthGood

    {

        background: blue;

    }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <ajaxControlToolkit:ToolkitScriptManager runat="server">

    </ajaxControlToolkit:ToolkitScriptManager>

    <h3>User Reigstration</h3>

    Email:<br />

    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />

    Password:<br />

    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>

    <ajaxControlToolkit:PasswordStrength ID="txtPassword_PasswordStrength" runat="server"

        TargetControlID="txtPassword" DisplayPosition="BelowRight"

        PrefixText="Your password is " StrengthIndicatorType="Text"

        MinimumNumericCharacters="1" MinimumSymbolCharacters="1" PreferredPasswordLength="6"

        RequiresUpperAndLowerCaseCharacters="true" TextStrengthDescriptions="Weak; Not Good; Good"

        TextStrengthDescriptionStyles="TextStrengthWeak; TextStrengthNotGood; TextStrengthGood">

    </ajaxControlToolkit:PasswordStrength>

    <br />

    Confirm Password:<br />

    <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>

    <ajaxControlToolkit:PasswordStrength ID="txtConfirmPassword_PasswordStrength" runat="server"

        TargetControlID="txtConfirmPassword">

    </ajaxControlToolkit:PasswordStrength>

    <br />

    <asp:Button ID="btnRegister" runat="server" Text="Register" />

    <div>

    </div>

    </form>

</body>

</html>

Output

In the second case I use a bar indicator to show the visual strength of the password. I keep same settings for the number of characters in the password but change the indicator position to above right.

There are two other properties that I need to set to activate the bar indicator and they are BarBorderCssClass and BarIndicatorCssClass that get two CSS class names for the indicator border and the indicator, itself.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"

 Inherits="PasswordStrengthSample._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>PasswordStrength Sample</title>

    <style>

    .TextStrengthWeak

    {

        background: red;

    }

    .TextStrengthNotGood

    {

        background: yellow;

    }

    .TextStrengthGood

    {

        background: blue;

    }

    .BarBorder

    {

        border-style: dashed;

        border-width: 2px;

        width: 180px;

    }

    .BarIndicator

    {

        color: orange;

        background-color: orange;

    }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <ajaxControlToolkit:ToolkitScriptManager runat="server">

    </ajaxControlToolkit:ToolkitScriptManager>

    <h3>User Reigstration</h3>

    Email:<br />

    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />

    Password:<br />

    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>

    <ajaxControlToolkit:PasswordStrength ID="txtPassword_PasswordStrength" runat="server"

        TargetControlID="txtPassword" DisplayPosition="BelowRight"

        PrefixText="Your password is " StrengthIndicatorType="Text"

        MinimumNumericCharacters="1" MinimumSymbolCharacters="1" PreferredPasswordLength="6"

        RequiresUpperAndLowerCaseCharacters="true" TextStrengthDescriptions="Weak; Not Good; Good"

        TextStrengthDescriptionStyles="TextStrengthWeak; TextStrengthNotGood; TextStrengthGood">

    </ajaxControlToolkit:PasswordStrength>

    <br />

    Confirm Password:<br />

    <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>

    <ajaxControlToolkit:PasswordStrength ID="txtConfirmPassword_PasswordStrength" runat="server"

        TargetControlID="txtConfirmPassword" DisplayPosition="AboveRight"

        StrengthIndicatorType="BarIndicator" MinimumNumericCharacters="1"

        MinimumSymbolCharacters="1" PreferredPasswordLength="6"

        RequiresUpperAndLowerCaseCharacters="true"

        BarBorderCssClass="BarBorder" BarIndicatorCssClass="BarIndicator">

    </ajaxControlToolkit:PasswordStrength>

    <br />

    <asp:Button ID="btnRegister" runat="server" Text="Register" />

    <div>

    </div>

    </form>

</body>

</html>

Output 

The last point to mention is, this control is just a visual indicator of password strength and can't guarantee it because it just shows the user how secure is his password. You need to implement your own validation mechanism to block poor passwords.

You can download the sample code for this post from here.

[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.

1 Comment : 03.23.08

Feedbacks

 avatar
#1
Girish Singh
04.04.2008 @ 7:34 AM

It is true that many of these controls are not actually much covered in many books. Nice code I must say. Another article that I found with a simple application of ajax toolkit was on

http://www.mabaloo.com/Web-Development/AJAX-with-ASP.NET-using-Visual-Studio-2005-and-AJAX-Toolkit.html

Here the author creates a simple employee database application was a web service and uses ajax on it

Leave a Comment