private bool ManagePartOne(LocalPasswordModel model, out ActionResult actionResult) { if (ModelState.IsValid) { // ChangePassword will throw an exception rather than return false in certain failure scenarios. bool changePasswordSucceeded; try { changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword); } catch (Exception) { changePasswordSucceeded = false; } if (changePasswordSucceeded) { { actionResult = RedirectToAction("Manage", new {Message = ManageMessageId.ChangePasswordSuccess}); return true; } } ModelState.AddModelError("", "The current password is incorrect or the new password is invalid."); } actionResult = null; return false; }
private bool ManagePartTwo(LocalPasswordModel model, out ActionResult manage) { // 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 { WebSecurity.CreateAccount(User.Identity.Name, model.NewPassword); { manage = RedirectToAction("Manage", new {Message = ManageMessageId.SetPasswordSuccess}); return true; } } catch (Exception e) { ModelState.AddModelError("", e); } } manage = null; return false; }
public ActionResult Manage(LocalPasswordModel model) { bool hasLocalAccount = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name)); ViewBag.HasLocalPassword = hasLocalAccount; ViewBag.ReturnUrl = Url.Action("Manage"); if (hasLocalAccount) { ActionResult actionResult; if (ManagePartOne(model, out actionResult)) return actionResult; } else { ActionResult manage; if (ManagePartTwo(model, out manage)) return manage; } // If we got this far, something failed, redisplay form return View(model); }