Reputation: 1014
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
Upvotes: 1
Views: 568
Reputation: 26940
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();
Upvotes: 1