Search code examples
asp.netmembership

ASP.NET Membership validate password


I use ASP.NET Membership to manage users on my site. But I need to create the user accounts manually, without the help of the CreateUserWizard. I have set textboxes for the required fields, and now I am setting up validators.

The question is: How do I make a validator that would see if the password the user enters in the textbox is valid?

I know that there is a specific format the password must have, but that can be changed, and I would like for the validator to work even if that changes. So a regular expression validator won't work (i think)


Solution

  • There doesn't seem to be a simple way. However, you can use the Membership.CreateUser that takes a MembershipCreateStatus param.

    If the password is valid, the created user object will be null and the MembershipCreateStatus will be set to InvalidPassword (or any other creation status).

    Example:

    MembershipCreateStatus membershipCreateStatus;
    MembershipUser newUser = Membership.CreateUser(userName, password, email, passwordQuestion, passwordAnswer, true, out membershipCreateStatus);
    
    // Check if the user was created succesfully
    if (newUser == null)
    {
        // membershipCreateStatus contains the information why the creation was not successful
        if (membershipCreateStatus == MembershipCreateStatus.InvalidPassword)
        {
            // The password doesn't match the requirements
        }
    }