asp.net membership change password without knowing old one
Asked Answered
D

10

60

Evaluting the method signature, it is required to know old password while changing it.

membershipUser.ChangePassword(userWrapper.OldPassword, userWrapper.Password)

Is there any way to change password without knowing old one.

Dareen answered 16/2, 2011 at 8:1 Comment(0)
M
117
 string username = "username";
 string password = "newpassword";
 MembershipUser mu = Membership.GetUser(username);
 mu.ChangePassword(mu.ResetPassword(), password);
Microeconomics answered 16/2, 2011 at 8:6 Comment(4)
Note that enablePasswordReset option must be enabled in web.config membership provider configurationSanorasans
Why can't you just use mu.GetPassword(); instead of mu.ResetPassword(); ?Tautologism
@RMiranda you could use GetPassword but only if you're storing encrypted passwords in the database when you should be hashing them: security.blogoverflow.com/2011/11/…Immersionism
ResetPassword() returns new password. But first parameter of ChangePassword() represents old password. I don't understand. How to exactly works ResetPassword() method?Shanel
I
24

The other answers here are correct, but can leave the password in an unknown state.

ChangePassword will throw exceptions if the password doesn't meet the requirements laid out in Web.Config (minimum length, etc.). But it only fails after ResetPassword has been called, so the password will not be known to the original user or to the person who's tried to change it. Check for complexity requirements before changing the password to avoid this:

var user = Membership.GetUser(userName, false);

if ((newPassword.Length >= Membership.MinRequiredPasswordLength) &&
    (newPassword.ToCharArray().Count(c => !Char.IsLetterOrDigit(c)) >=
         Membership.MinRequiredNonAlphanumericCharacters) &&
    ((Membership.PasswordStrengthRegularExpression.Length == 0) ||
         Regex.IsMatch(newPassword, Membership.PasswordStrengthRegularExpression))) {

    user.ChangePassword(user.ResetPassword(), newPassword);
} else {
    // Tell user new password isn't strong enough
}
Immersionism answered 14/5, 2014 at 12:33 Comment(1)
Even today that is a valid and insightful comment (for legacy code that I'm working on). Thank you.Vaca
R
14

You need to reset the user's password before changing it, and pass in the generated password to ChangePassword.

string randompassword = membershipUser.ResetPassword();
membershipUser.ChangePassword(randompassword , userWrapper.Password)

or inline:

membershipUser.ChangePassword(membershipUser.ResetPassword(), userWrapper.Password)
Rex answered 16/2, 2011 at 8:5 Comment(3)
Why can't you just use membershipUser.GetPassword(); instead of membershipUser.ResetPassword(); ?Tautologism
@RMiranda - because passwords should never be stored in a retrievable way. They should be hashed instead. See msdn.microsoft.com/en-us/library/2x0c6sfa(v=vs.110).aspx and en.wikipedia.org/wiki/…Rex
ResetPassword() returns new password. But first parameter of ChangePassword() represents old password. I don't understand. How to exactly works ResetPassword() method?Shanel
K
4

Try to use SimpleMembershipProvider it's easier:

var token = WebSecurity.GeneratePasswordResetToken("LoginOfUserToChange");
WebSecurity.ResetPassword(token, "YourNewPassword");
Kalidasa answered 9/9, 2013 at 11:43 Comment(0)
M
4

Please note, all these mentioned solutions will only work if the RequiresQuestionAndAnswer property is set to false in Membership system configuration. If RequiresQuestionAndAnswer is true then the ResetPassword method needs to be passed the security answer, otherwise it will throw an exception.

In case you need RequiresQuestionAndAnswer set to true, you can use this workaround

Maryland answered 4/12, 2013 at 18:58 Comment(0)
P
2

This code mentioned on posts above is working:

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

But you have to set requiresQuestionAndAnswer="false" in web.config in membership provider tag. If it is true, resetpassword method generate an error "Value can not be null". In this case you must supply question answer as parameter to ResetPassword.

Prud answered 11/7, 2017 at 8:31 Comment(0)
R
1

Use the password you want to set from textbox in place of 123456.

 MembershipUser user;     
 user = Membership.GetUser(userName,false);
 user.ChangePassword(user.ResetPassword(),"123456");
Restful answered 1/3, 2012 at 5:43 Comment(0)
V
0

@Rob Church is right:

The other answers here are correct but can leave the password in an unknown state.

However, instead of his solution to do the validation by hand, I would try to change the password using the ResetPassword from token method and catch and show the error:

var user = UserManager.FindByName(User.Identity.Name);
string token = UserManager.GeneratePasswordResetToken(user.Id);
var result = UserManager.ResetPassword(user.Id, token, model.Password);
if (!result.Succeeded){
    // show error
}
Valonia answered 30/4, 2017 at 7:59 Comment(1)
this topic here is to use old Membership. But your code is from new Asp.Net Identity. :)Heck
C
0
string username = "UserName";
string userpassword = "NewPassword";
string resetpassword;
    
MembershipUser mu = Membership.GetUser(username, false);

if (mu == null){
    Response.Write("<script>alert('Invalid Username!')</script>"); 
}

else{
    resetpassword = mu.ResetPassword(username);
    if (resetpassword != null){
         if (mu.ChangePassword(resetpassword, userpassword)){
             Response.Write("<script>alert('Password changed successfully!')</script>"); 
         }
    }
    else{
           Response.Write("<script>alert('Oh some error occurred!')</script>"); 
        }
    }
Cavalryman answered 28/7, 2020 at 15:3 Comment(1)
Welcome to StackOverflow. Please provide some explanation as well.Michaud
C
0
 string username = "UserName";
 string userpassword = "NewPassword";   
 MembershipUser mu = Membership.GetUser(username, false);
 mu.ChangePassword(mu.ResetPassword(username), userpassword);
Cavalryman answered 28/7, 2020 at 15:18 Comment(1)
Was this meant as an edit to your other answer? Use the edit link underneath the post and delete this one.Devon

© 2022 - 2025 — McMap. All rights reserved.