Identityuser Password Change - Aspnet

Asked

Viewed 498 times

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.

1 answer

1


Just use the method ChangePasswordAsync.

In the ASP.NET Core use that way:

ApplicationUser appUser = db.Users.Find(userId);
var result = await UserManager.ChangePasswordAsync(appUser, model.NewPassword);

Below is an example of password change method using Identity:

public async Task<ActionResult> ChangePassword(ChangePasswordViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    var userId = User.Identity.GetUserId();
    var result = await UserManager.ChangePasswordAsync(userId, model.OldPassword, model.NewPassword);
    if (result.Succeeded)
    {
        var user = await UserManager.FindByIdAsync(userId);
        if (user != null)
        {
            await SignInAsync(user, isPersistent: false);
        }
        return RedirectToAction("Index", new { Message = ManageMessageId.ChangePasswordSuccess });
    }
    AddErrors(result);
    return View(model);
}
  • Oh yes, I get it, but I can’t do it in the form I already use to change ? Is it possible ?

  • @marianac_costa Can yes, this is just an example, just need to adapt to your case.

  • George, I’m trying to do it, but he always falls in the false, I changed the way I caught the Id for ApplicationUser 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''

  • @marianac_costa, now that I see that you have the core tbm tag, I will edit.

  • That way you passed tb returned error, I tried so ApplicationUser appUser =await _userManager.GetUserAsync(User);&#xA; 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 functions ApplicationUser user = await _userManager.GetUserAsync(User); tried so, and yet he returns false

  • 1

    Your answer helped me get the correct answer, I had to change is the HashPassword thus: var newPassword=_userManager.PasswordHasher.HashPassword(user,newpass);&#xA; user.PasswordHash = newPassword;&#xA; var res =await _userManager.UpdateAsync(user); thank you.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.