1

I was writing code for changing the passwords but I don't know the reason why the data is not updated to the database. I have built custom membership table and membership Provider class. Here enitiy named IVRControlPanelEntities is generated from entity framework. I have following function to change password.

 public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            IVRControlPanelMembershipProvider memberservice = new IVRControlPanelMembershipProvider();
            if (!memberservice.ValidateUser(username, oldPassword) || string.IsNullOrEmpty(newPassword.Trim()))
            {
                return false;
            }
            else
            {
                using (IVRControlPanelEntities db = new IVRControlPanelEntities())
                {
                    User user = GetUser(username);
                   // string hash = FormsAuthentication.HashPasswordForStoringInConfigFile(newPassword.Trim(), "md5");
                    string PasswordSalt = CreateSalt();
                    user.PasswordSalt = PasswordSalt;
                    user.Password = CreatePasswordHash(newPassword, PasswordSalt);
                    db.SaveChanges();


                }
               return true;
            }

        }

Problems: I try changing the password, all is working fine but data for new password is not updated to the table. I have also searched for reason and use following code: 1.

db.Entry(user).State = EntityState.Modified;

2.

db.Users.Attach(user);
                 db.ApplyCurrentValues("Users", user);

None of above is working and also use TryUpdateModel() function but it's not detecting .

It may be due to state of object is not defined, how can I solve this problem

1 Answer 1

1

If method GetUser uses different context, then data will not be updated.

Try this:

User user = db.Users.FirstOrDefault(x=>x.UserName == username);
               // string hash = FormsAuthentication.HashPasswordForStoringInConfigFile(newPassword.Trim(), "md5");
                string PasswordSalt = CreateSalt();
                user.PasswordSalt = PasswordSalt;
                user.Password = CreatePasswordHash(newPassword, PasswordSalt);
                db.SaveChanges();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks karaxuna now it shows effect in the database thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.