public async Task <ActionResult> ManagePassword(ManagePasswordViewModel model)
        {
            bool hasPassword = HasPassword();

            ViewBag.HasLocalPassword = hasPassword;
            if (hasPassword)
            {
                if (ModelState.IsValid)
                {
                    IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);

                    if (result.Succeeded)
                    {
                        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));
        }
Beispiel #2
0
        public async Task<ActionResult> ManagePassword(ManagePasswordViewModel model)
        {
            bool hasPassword = HasPassword();
            ViewBag.HasLocalPassword = hasPassword;

            if (hasPassword)
            {
                if (ModelState.IsValid)
                {
                    IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.Password);
                    if (result.Succeeded)
                    {
                        TempData["Message"] = "Your password has been changed.";
                        return RedirectToAction("Manage");
                    }
                }
                else
                {
                    EditErrorMessageConfirmPassword();
                    if (ModelState["OldPassword"].Errors.Count != 0)
                    {
                        ModelState["Password"].Errors.Clear();
                        ModelState["ConfirmPassword"].Errors.Clear();
                    }
                }
            }
            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.Password);
                    if (result.Succeeded)
                    {
                        TempData["Message"] = "Your new password has been set.";
                        return RedirectToAction("Manage");
                    }
                }
                else
                {
                    EditErrorMessageConfirmPassword();
                }
            }
            // Pass and process model errors.
            TempData["PasswordViewData"] = ViewData;
            return RedirectToAction("Manage");
        }