Change Passord

Posted by Community Admin on 04-Aug-2018 15:36

Change Passord

All Replies

Posted by Community Admin on 20-Sep-2016 00:00

I am failing on the save.   It says I am not authorized to manage users.....

 

Web Form Code

        public static bool ChangePassword(string username, string newPassword, string oldPassword)
       
            UserManager userManager = UserManager.GetManager(ProviderName);

            User user = userManager.GetUser(username);

            bool result = userManager.ChangePassword(user, oldPassword, newPassword);

            if (result)
           
                userManager.SaveChanges();
           

            return result;
       

 

Custom Membership Provider Code

        public override bool ChangePassword(User user, string oldPassword, string newPassword)
       
            var userManager = UserManager.GetManager("Default");

            return userManager.ChangePassword(user, oldPassword, newPassword);

            throw new NotSupportedException();
       

Posted by Community Admin on 20-Sep-2016 00:00

Hi Joseph.,

This is the working example:

public static bool ChangePassword(string username, string newPassword, string oldPassword)
       
           UserManager userManager = UserManager.GetManager(ProviderName);
           userManager.Provider.SuppressSecurityChecks = true;
 
           User user = userManager.GetUser(username);
 
           bool result = userManager.ChangePassword(user, oldPassword, newPassword);
 
           if (result)
           
 
               userManager.Provider.SuppressSecurityChecks = false;
               userManager.SaveChanges();
           
 
           return result;
       

 

Posted by Community Admin on 20-Sep-2016 00:00

This is my updated code.  It returns the user object with a new password.    I needed to set the Suppress Security Check to = true.

However when the userManager.SaveChanges() runs it runs without error, but it doesn't update the database with new password.  

 

        public static bool ChangePassword(string username, string newPassword, string oldPassword)
       

            UserManager userManager = UserManager.GetManager("CustomMembershipProvider");

            userManager.Provider.SuppressSecurityChecks = true;
            
            User user = userManager.GetUser(username);

            bool result = userManager.ChangePassword(user, oldPassword, newPassword);
            try
           
                if (result)
               
                    userManager.SaveChanges();
               

           
            catch (Exception ex)
           
                string exxx = ex.ToString();
           
            return result;
       

 

 

Posted by Community Admin on 20-Sep-2016 00:00

My snippet of code is working fine for me. I checked it recently. 

Can you please create short screencast, to prove that problem exists

Posted by Community Admin on 21-Sep-2016 00:00

Link for video  https://youtu.be/pRbFgZxRv_E

Thanks for your help Victor!!

Posted by Community Admin on 26-Sep-2016 00:00
As I noticed, in your video you didnt call userManager.SaveChanges() after
This thread is closed