ASP.NET Identity change password

前端 未结 10 1932
礼貌的吻别
礼貌的吻别 2020-11-29 18:09

I need ability to change password for user by admin. So, admin should not enter a current password of user, he should have ability to set a new password. I look at ChangePas

10条回答
  •  猫巷女王i
    2020-11-29 18:39

    EDIT: I know the OP requested an answer which performs the task in one transaction but I think the code is useful to people.

    All the answers use the PasswordHasher directly which isn't a good idea as you will lose some baked in functionality (validation etc).

    An alternative (and I would assume the recommended approach) is to create a password reset token and then use that to change the password. Example:

    var user = await UserManager.FindByIdAsync(id);
    
    var token = await UserManager.GeneratePasswordResetTokenAsync(user);
    
    var result = await UserManager.ResetPasswordAsync(user, token, "MyN3wP@ssw0rd");
    

提交回复
热议问题