public async Task <ActionResult> Manage(ManageUserViewModel model) { string userId = User.Identity.GetUserId(); bool hasLocalLogin = await IdentityStore.HasLocalLogin(userId); ViewBag.HasLocalPassword = hasLocalLogin; ViewBag.ReturnUrl = Url.Action("Manage"); if (hasLocalLogin) { if (ModelState.IsValid) { bool changePasswordSucceeded = await IdentityStore.ChangePassword(User.Identity.GetUserName(), model.OldPassword, model.NewPassword); if (changePasswordSucceeded) { return(RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess })); } else { ModelState.AddModelError("", "The current password is incorrect or the new password is invalid."); } } } else { // User does not have a local 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) { try { // Create the local login info and link the local account to the user if (await IdentityStore.CreateLocalLogin(userId, User.Identity.GetUserName(), model.NewPassword)) { return(RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess })); } else { ModelState.AddModelError("", "Failed to set password"); } } catch (Exception e) { ModelState.AddModelError("", e); } } } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IHttpActionResult> SetPassword(SetPasswordBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { // Create the local login info and link the local account to the user if (!await IdentityStore.CreateLocalLogin(User.Identity.GetUserId(), User.Identity.GetUserName(), model.NewPassword)) { return(BadRequest("Failed to set password.")); } } catch (IdentityException e) { return(BadRequest(e.ToString())); } return(OK()); }