Check Password Strength Using Ajax PasswordStrength Control in ASP.NET

Background

In modern application it becomes important to protect personal account information and for this their is an option known as password strength. In this article we will learn about the Ajax PasswordStrength Checker control which shows the message based on the password policy we have set.

The following are some of the common properties of the Ajax PasswordStrength control:

  • BarBorderCssClass: Used to set custom css class for border of PasswordStrength control.

  • BarIndicatorCssClass: Used to set custom css class for password strength Bar Indicator.

  • CalculationWeightings: Set the Calculation Weightings for strength Bar Indicator.

  • DisplayPosition: Sets the display position for strength indicator ,default is right.

  • HelpHandleCssClass: Sets the CSS class for help link.

  • HelpHandlePosition: Sets the help link position,default is AboveRight.

  • HelpStatusLabelID: sets the help status Lable Id.

  • MinimumLowerCaseCharacters: Defines the how many minimum lower case letter in password.

  • MinimumNumericCharacters: Defines how many numeric characters in password.

  • MinimumSymbolCharacters: Defines minimum occurrences of Special symbol characters.

  • MinimumUpperCaseCharacters: Defines the how many minimum upper case letter in password.

  • PreferredPasswordLength: Defines the length of the password.

  • PrefixText: Defines prefix for password which should be start from prefix.

  • RequiresUpperAndLowerCaseCharacters: Specifies whether the password must required both upper and lower case characters .if yes then required ,if no then not required.

  • StrengthIndicatorType: Defines the strenght indicator type whether it will be BarIndicator or Text.

  • StrengthStyles: Used to specify CSS class for strength indicator.

  • TextCssClass: Used to specify CSS class for text.

  • TextStrengthDescriptions: Used to specify text to describe password strength such as good,strong ,very strong etc.

  • TextStrengthDescriptionStyles: Used to specify CSS class for strength indicator text description.

I hope you got the basic Idea about the Ajax control Toolkit password strength control.

Now let us see the preceding explanation by creating a sample web application as in the following:

  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".

  2. "File" - "New Project" - "C#" - "Empty WebSite" (to avoid adding a master page).

  3. Provide the website a name: "UsingPasswordStrengthValidator" or another as you wish and specify the location.

  4. Then right-click on the Solution Explorer and select "Add New Item" and Add Web Form.

  5. Drag and drop one Button, a TextBox onto the <form> section of the Default.aspx page.

  6. Add the reference of Ajax Control toolkit from nuget or directly from the official website of Ajax Control toolkit. To add using the nuget package manager, type ajax control toolkit in NuGet package manager search and click on search button. Here's a screenshot for making things clear:

    ajax control toolkit

  7. After installing, the Ajax control will get added automatically into the toolbox. Now click on upper right hand corner of the textbox and add the PasswordStrength control to textbox control as in the following screenshot:

    PasswordStrength

Now the default.aspx page source code will look like the following:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>    
  2.     
  3. <!DOCTYPE html>    
  4.     
  5. <html xmlns="http://www.w3.org/1999/xhtml">    
  6. <head runat="server">    
  7.     <title></title>    
  8. </head>    
  9. <body>    
  10.     <form id="form1" runat="server">    
  11.         <div>    
  12.             <table style="margin-top: 30px">    
  13.                 <tr>    
  14.     
  15.                     <td>Enter Password    
  16.                     </td>    
  17.                     <td>    
  18.                         <asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox>    
  19.     
  20.                         <ajaxToolkit:PasswordStrength ID="txtPassword_PasswordStrength" runat="server" BehaviorID="txtPassword_PasswordStrength" TargetControlID="txtPassword" />    
  21.     
  22.                     </td>    
  23.                 </tr>    
  24.             </table>    
  25.         </div>    
  26.     
  27.     </form>    
  28. </body>    
  29. </html>   
Now let us set PasswordStrength control properties:
  • PreferredPasswordLength : 10
  • MinimumLowerCaseCharacters : 2
  • MinimumUpperCaseCharacters : 2
  • MinimumNumericCharacters : 2
  • RequiresUpperAndLowerCaseCharacters : True
  • StrengthIndicatorType : Text.

After setting the values for PasswordStrength control, the code will look like the following:

  1. <ajaxToolkit:PasswordStrength ID="txtPassword_PasswordStrength" runat="server"   
  2. BehaviorID="txtPassword_PasswordStrength" TargetControlID="txtPassword"  
  3.  MinimumLowerCaseCharacters="2" MinimumNumericCharacters="2"   
  4. MinimumUpperCaseCharacters="2" PreferredPasswordLength="10" />   
Now let us run the application, and enter some different text as we defined in properties. Here's the screenshot:

run the application
After entering one more character (.), the strength of the password will look like the following screenshot:

enter one more character
Now enter the passwords which is like the one defined under the properties of PasswordStrength control. After that it will show the following message:

Enter password
The complete Default.aspx code is given below:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>    
  2.     
  3. <!DOCTYPE html>    
  4.     
  5. <html xmlns="http://www.w3.org/1999/xhtml">    
  6. <head runat="server">    
  7.     <title></title>    
  8. </head>    
  9. <body>    
  10.     <form id="form1" runat="server">    
  11.         <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>    
  12.         <div>    
  13.             <table style="margin-top: 30px">    
  14.                 <tr>    
  15.     
  16.                     <td>Enter Password    
  17.                     </td>    
  18.                     <td>    
  19.                         <asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox>    
  20.     
  21.                         <ajaxToolkit:PasswordStrength StrengthIndicatorType="Text" ID="txtPassword_PasswordStrength" runat="server" BehaviorID="txtPassword_PasswordStrength" TargetControlID="txtPassword" MinimumLowerCaseCharacters="2" MinimumNumericCharacters="2" MinimumUpperCaseCharacters="2" PreferredPasswordLength="10" />    
  22.     
  23.                     </td>    
  24.                 </tr>    
  25.             </table>    
  26.         </div>    
  27.     
  28.     </form>    
  29. </body>    
  30. </html>   

From all the above examples we learned about the Password Strength control.

Notes 
  • Please add Ajax control toolkit dll reference as shown in this article.
  • Don't forget to give the reference of Script manager.
  • For more details and explanation, download the Uploaded Zip file.

Summary

From all the preceding examples you have learned about the Password Strength control. I hope this article is useful for all readers. If you have a suggestion then please contact me or mention it in the comments section.

Up Next
    Ebook Download
    View all
    Learn
    View all