Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to use minRequiredPasswordLength and minRequiredNonalphanumericCharacters?

How to use minRequiredPasswordLength and minRequiredNonalphanumericCharacters?

Scheduled Pinned Locked Moved ASP.NET
csharpsysadminhelptutorialquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kbalias
    wrote on last edited by
    #1

    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 the CreateUserWizard.

    <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

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups