Adnan’s Blog

ASP.net Reset Password

Posted on: July 6, 2008

If you are using Membership Provider in your ASP.net application, you might come across a scenario in which you need to have both Security Question and Answer feature and would also like to programmatically reset the password for an account.

So if you run the code

string username = “user”;
string password = “password”;
MembershipUser mu = Membership.GetUser(username);
mu.ChangePassword(mu.ResetPassword(), password);

You will get an error when you try to reset the password. A solution is to add another Membership provider having all the same settings as the default provider with only one exception:

<add name="NewMembershipProvider" requiresQuestionAndAnswer=”false” ....../>

For all Membership functions, you the default Membership provider will be used, but when you need to reset the password, you must reference the new Provider. Here’s a code excerpt to help you out:

string username = “user”;
string password = “password”;
MembershipUser mu = Membership.Providers["NewMembershipProvider"].GetUser(username, false);
mu.ChangePassword(mu.ResetPassword(), password);

So now your application can use both the Security feature to recover password and programmatically Change/Reset the password when required.

Advertisements

2 Responses to "ASP.net Reset Password"

Excellent tip!!!. Keep up the good work.

another solution is to break it out:
string reset = mu.ResetPassword;
mu.ChangePassword(reset, password);

worked for me but you helped get me there.
Thanks!

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: