57

Problem, there's no method:

bool ChangePassword(string newPassword);

You have to know the current password (which is probably hashed and forgotten).

Brian Kim
  • 24,916
  • 6
  • 38
  • 26
mcqwerty
  • 3,386
  • 2
  • 25
  • 24

2 Answers2

130

This is an easy one that I wasted too much time on. Hopefully this post saves someone else the pain of slapping their forehead as hard as I did.

Solution, reset the password randomly and pass that into the change method.

MembershipUser u = Membership.GetUser();
u.ChangePassword(u.ResetPassword(), "myAwesomePassword");
chakrit
  • 61,017
  • 25
  • 133
  • 162
mcqwerty
  • 3,386
  • 2
  • 25
  • 24
  • 2
    Hey, come on. Don't downvote for answering your own question. The FAQ encourages that. See http://stackoverflow.com/questions/18557/how-does-stackoverflow-work-the-unofficial-faq#119658 – DOK Nov 13 '08 at 16:00
  • The down-vote (not me!) was before the answer was moved here. Previously it said that the answer was in the question. I'm not too keen on down-voting people who have only just joined SO - it's much better to edit to help out. – harriyott Nov 13 '08 at 16:02
  • Yes and he gets the pretty badge too !! – CheGueVerra Nov 13 '08 at 16:44
  • @harriyott doh... I think next time one of us should leave a comment indicating we're editing... so we don't waste time doing the same thing. – chakrit Nov 13 '08 at 16:53
  • On my app i get this: Value cannot be null. Parameter name: passwordAnswer any ideas?? – Andrew Feb 17 '10 at 14:41
  • Make sure you have this attribute set in your web.config if you get the error above: requiresQuestionAndAnswer="true" – Andrew Feb 17 '10 at 15:13
  • 3
    I wasted a lot of time before I found this answer, thank you. – Frank Schwieterman May 05 '10 at 05:09
  • Can you use this with a textbox? I have been trying and can't get it to work for me – Jamie Sep 28 '12 at 13:24
  • +1 - kudos; went round the houses on using Membership.ChangePawword() on this too and then found this wee gem. – jim tollan Jan 11 '14 at 02:17
2

You are not able to change the password if the requiresQuestionAndAnswer="true"

I got the work around for this

Created two membership providers in web.config

i am using the AspNetSqlMembershipProviderReset provider for reseting the password since it has the requiresQuestionAndAnswer= false where as AspNetSqlMembershipProvider is the default provider used.

i wrote the following code to reset the password for the user.

public bool ResetUserPassword(String psUserName, String psNewPassword) { try { // Get Membership user details using secound membership provider with required question answer set to false.

        MembershipUser currentUser = Membership.Providers["AspNetSqlMembershipProviderReset"].GetUser(psUserName,false);

        //Reset the user password.
        String vsResetPassword = currentUser.ResetPassword();            

        //Change the User password with the required password            
        currentUser.ChangePassword(vsResetPassword, psNewPassword);
        //Changed the comments to to force the user to change the password on next login attempt
        currentUser.Comment = "CHANGEPASS";
        //Check if the user is locked out and if yes unlock the user
        if (currentUser.IsLockedOut == true)
        {
            currentUser.UnlockUser();
        }
        Membership.Providers["AspNetSqlMembershipProviderReset"].UpdateUser(currentUser);            return true;
    }
    catch (Exception ex)
    {
        throw ex;
        return false;
    }
}