Change Password Programmatically

Posted by Community Admin on 04-Aug-2018 08:33

Change Password Programmatically

All Replies

Posted by Community Admin on 07-Jun-2012 00:00

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

Posted by Community Admin on 11-Jun-2012 00:00

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;

Change password
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();



Regards,
Stanislav Velikov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 21-Oct-2015 00:00

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.

This thread is closed