Resetting Password with ASP.NET 2.0 Membership December 16, 2008
Posted by mcamail2002 in Uncategorized.trackback
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 mUser = Membership.GetUser(username); mu.ChangePassword(mUser.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:
<!– To Reset password of the user. –>
< membership defaultProvider=“SqlMembershipProvider“> < providers> < clear/> < add name=“SqlMembershipProviderAdmin“ type=“System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a“ applicationName=“/“ connectionStringName=“ConnectionString“ enablePasswordRetrieval=“true“ enablePasswordReset=“true“ requiresQuestionAndAnswer=“false“ requiresUniqueEmail=“true“ passwordFormat=“Encrypted“ maxInvalidPasswordAttempts=“5“ minRequiredPasswordLength=“1“ minRequiredNonalphanumericCharacters=“0“ passwordAttemptWindow=“50“ passwordStrengthRegularExpression=“”/> </ providers> </ membership>Then, when you change your password, reference it as follows:
string username = “username“;
string password = “pass@word“;
MembershipUser mUser = Membership.Providers["SqlMembershipProviderAdmin"].GetUser(username);
mu.ChangePassword(mUser.ResetPassword(), password);
Hope this helps.

Comments»
No comments yet — be the first.