Ove
Ove

Reputation: 6317

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)

Upvotes: 2

Views: 4682

Answers (3)

mbillard
mbillard

Reputation: 38842

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
    }
}

Upvotes: 3

Zhaph - Ben Duguid
Zhaph - Ben Duguid

Reputation: 26956

What's your objection to using (a variant) of the CreateUserWizard? If it's to do with needing additional fields, or the layout there are ways around that:

  1. How to: Customize the CreateUserWizard - Check out the section on "To customize the CreateUserWizard steps".
  2. ASP.NET 2.0 CSS Friendly Control Adaptors (these are going to be built into ASP.NET 4.0)

But even the default CreateUserWizard doesn't do client side password validation, I guess for this very reason :(

Upvotes: 0

Serhat Ozgel
Serhat Ozgel

Reputation: 23766

You can dynamically set the regex of a regular expression validator.

private void Page_Load(object sender, EventArgs e)
{
    passwordValidator.ValidationExpression = someConfiguration.GetPasswordValidationExpression();
}

Upvotes: 0

Related Questions