public ActionResult Manage(LocalPasswordModel model)
{
ViewBag.HasLocalPassword = this.membership.HasPassword(User.Identity.Name);
ViewBag.ReturnUrl = Url.Action("Manage");
if (ViewBag.HasLocalPassword)
{
if (ModelState.IsValid)
{
// ChangePassword will throw an exception rather than return false in certain failure scenarios.
bool changePasswordSucceeded;
try
{
changePasswordSucceeded = this.membership.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword);
}
catch (Exception)
{
changePasswordSucceeded = false;
}
if (changePasswordSucceeded)
{
return this.RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
}
else
{
ModelState.AddModelError(string.Empty, "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)
{
this.membership.SetPassword(User.Identity.Name, model.NewPassword);
return this.RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
}
}
// If we got this far, something failed, redisplay form
return this.View(model);
}