2
I have a registration form, which registers the user with email, and the password, but the need arose to change this password, and using the class
Identity
, she owns the field PasswordHash
, and I wish I could change the registered password. It is possible to change ?
This is the way I change the other data:
public async Task<IActionResult> Register(EditViewModel model)
{
var teste = model.Usuarios.Email;
var userInDb = db.Users.Where(u => u.Email.ToLower().Equals(model.Usuarios.Email.ToLower())).FirstOrDefault();
userInDb.PasswordHash = model.Usuarios.PasswordHash;
userInDb.FirstName = model.Usuarios.FirstName;
userInDb.LastName = model.Usuarios.LastName;
userInDb.PhoneNumber = model.Usuarios.PhoneNumber;
db.Update(userInDb);
await db.SaveChangesAsync();
//StatusMessage = "A conta do perfil foi atualizada";
return RedirectToAction("Index", "User");
}
However I do not know how to change the password, I searched and I found nothing until then explaining.
Oh yes, I get it, but I can’t do it in the form I already use to change ? Is it possible ?
– Mariana
@marianac_costa Can yes, this is just an example, just need to adapt to your case.
– George Wurthmann
George, I’m trying to do it, but he always falls in the
false
, I changed the way I caught theId
forApplicationUser userId = await _userManager.GetUserAsync(User);
, pq qwhile trying this way you gave me it returns this error:'Iidentity' does not contain a definition for 'Getuserid' and could not find any accessible 'Getuserid' extension method that accepts a first argument of type 'Iidentity''– Mariana
@marianac_costa, now that I see that you have the core tbm tag, I will edit.
– George Wurthmann
That way you passed tb returned error, I tried so
ApplicationUser appUser =await _userManager.GetUserAsync(User);
 var result = await _userManager.ChangePasswordAsync(appUser, model.Change.OldPassword, model.Change.NewPassword);
this way its, I didn’t understand very well. When I catch the user I do so in other functionsApplicationUser user = await _userManager.GetUserAsync(User);
tried so, and yet he returnsfalse
– Mariana
Your answer helped me get the correct answer, I had to change is the
HashPassword
thus:var newPassword=_userManager.PasswordHasher.HashPassword(user,newpass);
 user.PasswordHash = newPassword;
 var res =await _userManager.UpdateAsync(user);
thank you.– Mariana