Home > ASP.Net > Resetting Password with ASP.NET 2.0 Membership

Resetting Password with ASP.NET 2.0 Membership


If you ever have wanted to be able to programmatically change (reset) a users password while at the same time continuing to be

able to use the question and answer feature, this post is for you.  The problem is that if you use code like this:

 string username = "username";
 string password = "pass@word";
 MembershipUser mu = Membership.GetUser(username);
 mu.ChangePassword(mu.ResetPassword(), password);

You will find that if you have in your web.config requiresQuestionAnswer=”true”, you will get an error when you try and reset the

password.  The elegant solution to this is to create an additional membeship tag in your web.config and reference it when you

change passwords.  That is, add another provider like this:

<membership defaultProvider="SqlMembershipProvider" userIsOnlineTimeWindow="15">
    <add name="SqlMembershipProviderOther" type="SqlProviderOneShot.SqlMembershipProvider"
     connectionStringName="ConnectionString" applicationName="/"
    enablePasswordRetrieval="false" enablePasswordReset="true"
    requiresUniqueEmail="true" passwordFormat="Hashed"
    minRequiredNonalphanumericCharacters="0" writeExceptionsToEventLog="false"
    minRequiredPasswordLength="1" passwordStrengthRegularExpression=""
    passwordAttemptWindow="10" maxInvalidPasswordAttempts="8"/>

Then, when you change your password, reference it as follows:

string username = "username";
string password = "pass@word";
MembershipUser mu = Membership.Providers["SqlMembershipProviderOther"].GetUser(username);
mu.ChangePassword(mu.ResetPassword(), password);

Hope this helps

Good Luck

Categories: ASP.Net Tags:
  1. June 14, 2014 at 2:42 pm

    I’ve read some good stuff here. Definitely price bookmarking for revisiting.
    I surprise how much effort you set to create one of these magnificent informative site.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: