public ActionResult Manage(ManageMessageId? message) { var Db = DatabaseFactory.Get(); ViewBag.StatusMessage = message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed." : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set." : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed." : message == ManageMessageId.Error ? "An error has occurred." : ""; var id = User.Identity.GetUserId(); var user = Db.Users.First(x => x.Id == id); ViewBag.HasLocalPassword = HasPassword(user); ViewBag.ReturnUrl = Url.Action("Manage"); if (user == null) { return HttpNotFound(); } ManageUserViewModel model = new ManageUserViewModel(); model.Action = "LinkLogin"; model.ReturnUrl = "Manage"; ModelState old = ModelState["OldPassword"]; if (old != null) old.Errors.Clear(); ModelState blah = ModelState["NewPassword"]; if (blah != null) blah.Errors.Clear(); return View(model); }
public async Task<ActionResult> Manage(ManageUserViewModel model) { var Db = DatabaseFactory.Get(); var id = User.Identity.GetUserId(); var user = Db.Users.First(x => x.Id == id); bool hasPassword = HasPassword(user); ViewBag.HasLocalPassword = hasPassword; ViewBag.ReturnUrl = Url.Action("Manage"); if (hasPassword) { if (ModelState.IsValid) { IdentityResult result = await UserManager.ChangePasswordAsync(user.Id, model.OldPassword, model.NewPassword); if (result.Succeeded) { user.LastPasswordChangedDate = DateTime.Today; Db.Entry(user).State = System.Data.Entity.EntityState.Modified; await Db.SaveChangesAsync(); return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess }); } else { AddErrors(result); } } } else { // User does not have a password so remove any validation errors caused by a missing OldPassword field ModelState state = ModelState["OldPassword"]; if (state != null) { state.Errors.Clear(); } if (ModelState.IsValid) { IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword); if (result.Succeeded) { return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess }); } else { AddErrors(result); } } } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult Manage(ManageMessageId? message) { MacheteContext Db = DatabaseFactory.Get(); string currentUserId = User.Identity.GetUserId(); ViewBag.StatusMessage = message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed." : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set." : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed." : message == ManageMessageId.Error ? "An error has occurred." : ""; // Retrieve user record ApplicationUser user = Db.Users.First(x => x.Id == currentUserId); ViewBag.HasLocalPassword = HasPassword(user); ViewBag.ReturnUrl = Url.Action("Manage"); if (user == null) { return HttpNotFound(); } ManageUserViewModel model = new ManageUserViewModel(); model.Action = "LinkLogin"; model.ReturnUrl = "Manage"; ModelState oldPassword = ModelState["OldPassword"]; if (oldPassword != null) { oldPassword.Errors.Clear(); } ModelState newPassword = ModelState["NewPassword"]; if (newPassword != null) { newPassword.Errors.Clear(); } return View(model); }