Change Password Programmatically
Using: Sitefinity 5.
Hi there!
I am currently working on a User Module whereby it can allow user to change their password when logged on. Its a very classic example of "Old Password, New Password, Retype New Password".
So there, I need to replace the new password with the existing password. Any suggestion on that?
By the way, Ive tried the Change Password widget that is available, but it does not seems to be working. I'm seeing empty widget when I published it.
Thanks alot in advance!
Jarrod
Hello Jarrod,
Here is the API to change password
You will have to logged in to execute the API or authenticate a user with permissions to modify users with this API
// log the admin
UserManager manager = UserManager.GetManager();
var objUser = manager.GetUser(
"admin"
);
var validate = SecurityManager.AuthenticateUser(UserManager.GetDefaultProviderName(),
"admin"
,
"password"
,
false
);
bool
authenticated = validate == UserLoggingReason.Success;
var userManager = UserManager.GetManager(
"Default"
);
// to get old password you must first get the user so you can get its password or other properties
var getUser = userManager.Getuser(
new
Guid(
"2a7339fe-20ee-4a7d-8869-79a4ee4abc45"
)).Email;
// you can reset the pass
var reset = userManager.ResetPassword(
new
Guid(
"2a7339fe-20ee-4a7d-8869-79a4ee4abc45"
),
"123"
);
//UserManager.ChangePasswordForUser(userManager, new Guid("2a7339fe-20ee-4a7d-8869-79a4ee4abc45"), "password", "password2", true);
// of change it with this
var change = userManager.ChangePassword(
new
Guid(
"2a7339fe-20ee-4a7d-8869-79a4ee4abc45"
),
"password"
,
"password2"
);
userManager.SaveChanges();
If you tried this code and it didn't worked out, it's the expected behavior. Just replace the "password" string with the reset variable, that actually is the old password after reset, and it will work.