Change Passord - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Change Passord

  • Change Passord
  • 77f1541e-37c1-6deb-a958-ff0000446526_sf_passwordchange.jpg

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

  • 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;
           

     

  • 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;
           

     

     

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

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

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

    Thanks for your help Victor!!

  • As I noticed, in your video you didnt call userManager.SaveChanges() after