How to use minRequiredPasswordLength and minRequiredNonalphanumericCharacters?
-
Hi I am using Visual Web Developer 2010 Express (with C# as code-behind) to develop a website. I have created a custom MembershipProvider and I have specified that in the web.config that
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"I am not sure where and how to use these values. I have done the following: I have added a
CustomValidator
to the Password textbox in theCreateUserWizard
.<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
<asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator_PW" runat="server" ControlToValidate="Password"
OnServerValidate="CustomValidate_PW" ValidateEmptyText="true" Display="Dynamic"
ErrorMessage="" ForeColor="Red" ValidationGroup="RegisterUser">
</asp:CustomValidator>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required."
ValidationGroup="RegisterUser">*</asp:RequiredFieldValidator>And a server-side method in the code-behind:
protected void CustomValidate_PW(object source, ServerValidateEventArgs args)
{
CustomValidator cv = (CustomValidator)source;//check if password conforms to minimum password length string pw = RegisterUser.Password; if (pw.Length < Membership.MinRequiredPasswordLength) { cv.ErrorMessage = "Minimum Password length is " + Membership.MinRequiredPasswordLength; args.IsValid = false; } //check if password conforms to required number of nonAlphanumericCharacters if (Membership.MinRequiredNonAlphanumericCharacters > 0) { int count = 0; for (int j = 0; j < pw.Length; j++) { if (!char.IsLetterOrDigit(pw\[j\])) { count++; } } if (count < Membership.MinRequiredNonAlphanumericCharacters) { cv.ErrorMessage = "Password requires " + Membership.MinRequiredNonAlphanumericCharacters + " non-Aphanumeric characters"; args.IsValid = false; } }
}
Is there a better way or better place to do this? All help will be appreciated. Kobus