I need to change the password for the admin user. Thus, the administrator should not enter the current user password, he should be able to set a new password. I am looking at the ChangePasswordAsync method, but the old password is required for this method. Therefore, this method is not suitable for this task. So I did it like this:
[HttpPost] public async Task<ActionResult> ChangePassword(ViewModels.Admin.ChangePasswordViewModel model) { var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); var result = await userManager.RemovePasswordAsync(model.UserId); if (result.Succeeded) { result = await userManager.AddPasswordAsync(model.UserId, model.Password); if (result.Succeeded) { return RedirectToAction("UserList"); } else { ModelState.AddModelError("", result.Errors.FirstOrDefault()); } } else { ModelState.AddModelError("", result.Errors.FirstOrDefault()); } return View(model); }
It works, but theoretically we can get an error in the AddPasswordAsync method. Thus, the old password will be deleted, but the new one will not be set. This is not good. Any way to do this in a "single transaction"? PS. I saw the ResetPasswordAsync method with the reset token, it seems to be more secure (because it cannot be an unstable situation with the user), but in any case it performs 2 actions.
c # asp.net-mvc-5 asp.net-identity-2 reset-password
Oleg Sh Mar 27 '15 at 0:36 2015-03-27 00:36
source share