Asp.Net MVC Identity Reset Password

Viewed 34

Reset Password by Admin - Without Email Link Only Admin can Reset Password, I get this code from MVC Admin reset of User Password but in my project, I get an error on the line

await store.SetPasswordHashAsync(cUser, hashedNewPassword);

Code:

    //
    // POST: /Account/ResetPassword
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model)
    {
        ApplicationDbContext context = new ApplicationDbContext();
        UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context);
        UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser> (store);

        string userId = model.Id;
        string newPassword = model.NewPassword;

        string hashedNewPassword = UserManager.PasswordHasher.HashPassword(newPassword);

        ApplicationUser cUser = await store.FindByIdAsync(userId);

        await store.SetPasswordHashAsync(cUser, hashedNewPassword);
        await store.UpdateAsync(cUser);

        return RedirectToAction("Index");
    }

enter image description here

View code of reset pass word

1 Answers

EDIT: after seeing the view, the thing you can do is check if the view is actually getting an ID. Check the controller for the [get] action if it gives an id, one thing I do to check that is to temporarily change:

@Html.HiddenFor()

to

@Html.TextBoxFor()

then do a refresh.

You can also check the value of id in the breakpoint window if you are on debugging mode

Related