public ActionResult ChangedPassword(string userId = null, string resetPw = null) { ChangePasswordModel objChangePasswordViewModel = new ChangePasswordModel(); ApplicationDbContext db = new ApplicationDbContext(); var user = db.Users.Find(userId); if (resetPw == "true") { if (Session["CurrentUser"] == null) { Session["CurrentUser"] = User.Identity.Name; } } objChangePasswordViewModel.OldPassword = user.Password; return View(objChangePasswordViewModel); }
public async Task<ActionResult> ChangedPassword(ChangePasswordModel model) { if (!ModelState.IsValid) { return View(model); } var result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword); if (result.Succeeded) { ApplicationDbContext db = new ApplicationDbContext(); var user = db.Users.Find(User.Identity.GetUserId()); if (user != null) { await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); } user.Password = model.NewPassword; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); user.PasswordHash = DBHandler.getHashPW(user.Id); DBHandler.SaveUserLogged(user, null, null, UserLoggedActions.PasswordChanged); return RedirectToAction("Index", "Home"); } AddErrors(result); return View(model); }