实例说明 智能密码强度检测提供功能非常实用,而且非常重要的一项功能,它能够提示用户所输入密码安全性的强弱,如图18.3所示。 图18.3 密码强度检测 技术要点 PasswordStrength控件是ASP.NET AJAX Control Toolkit版本里面提供的一个检测密码强度控件,当用户在密码框中输入密码时,文本框的后面会有一个显密码强度提示,这种提示有两种方式:文本和进度条。提示信息的位置也可以由程序人员自己设置。另外,当密码框失去焦点时提示信息会自动消失。表18.3为PasswordStrength控件的主要属性及说明。 18.3 PasswordStrength控件的主要属性及说明 属性 | 说明 | TargetControlID | 要检测密码的TextBox控件ID | DisplayPosition | 密码强度提示的信息的位置,如:DisplayPosition="RightSide|LeftSide| BelowLeft" | StrengthIndicatorType | 强度信息提示方式,包括文本和进度条StrengthIndicatorType="Text| BarIndicator" | PreferredPasswordLength | 密码的长度 | PrefixText | 用文本方式时开头的文字PrefixText="强度:" | TextCssClass | 用文本方时文字的CSS样式 | MinimumNumericCharacters | 密码中最少要包含的数字数量 | MinimumSymbolCharacters | 密码中最好要包含的符号数量(*,#) | RequiresUpperAndLowerCaseCharacters | 是否需要区分大小写 | TextStrengthDescriptions | 文本方式时的文字提示信息 TextStrengthDescriptions="极弱;弱;中等;强;超强" | BarIndicatorCssClass | 进度条的CSS样式 | BarBorderCssClass | 进度条边框的CSS样式 | HelpStatusLabelID | 帮助提示信息的Lable控件ID | CalculationWeightings | 密码组成部门所占的比重,其值的格式为“A;B;C;D”。其中A表示长度比重,B表示数字的比重,C表示大写的比重,D表示特殊符号的比重。A、B、C、D四个值的和必须为100,默认值为“50;15;15;20” |
实现过程 (1)新建一个AJAX网站,将其命名为Ex08_03,默认主页为Default.aspx。 (2)在Default.aspx页中主要添加一个ScriptManager控件、一个PasswordStrength控件和一个TextBox控件,其中ScriptManager控件主要用于管理Web页面中的AJAX控件,PasswordStrength控件实现密码强度提示功能,TextBox控件输入要验证的密码文本。 (3)在Head标记中添加进度条样式。代码如下: <style type="text/css"> .bartype { color:blue; background-color:green; } .barborder { border-style:solid; border-width:1px; width:200px; vertical-align:middle; } .aaa { background-color:#047AFD; color:#ffffff; font-family:Arial; font-size:9pt; padding: 2px 3px 2px 3px; } </style> (4)设置PasswordStrength控件的属性及对应值,其代码如下: <cc1:PasswordStrength ID="PasswordStrength1" runat="server" TargetControlID="TextBox1" DisplayPosition="RightSide" TextCssClass="aaa" HelpHandlePosition="BelowLeft" MinimumNumericCharacters="2" MinimumSymbolCharacters="2" StrengthIndicatorType="BarIndicator" PrefixText="密码强度:" PreferredPasswordLength="10" RequiresUpperAndLowerCaseCharacters="true" TextStrengthDescriptions="很差;差;一般;好;很好" CalculationWeightings="40;20;20;20" BarIndicatorCssClass="bartype" BarBorderCssClass="barborder"> </cc1:PasswordStrength> |