Posted by: adnanrashid 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.
July 7, 2008 at 4:07 pm
Excellent tip!!!. Keep up the good work.