Exemplo n.º 1
0
        public IActionResult ChangeMyPassword(PasswordChangeModel model)
        {
            if (ModelState.IsValid)
            {
                AppUser user = CurrentUser;

                if (user != null)
                {
                    bool exist = Usermanager.CheckPasswordAsync(user, model.PasswordOld).Result;
                    if (exist)
                    {
                        IdentityResult result = Usermanager.ChangePasswordAsync(user, model.PasswordOld, model.PasswordNew
                                                                                ).Result;
                        if (result.Succeeded)
                        {
                            Usermanager.UpdateSecurityStampAsync(user);
                            SignInManager.SignOutAsync();
                            SignInManager.PasswordSignInAsync(user, model.PasswordNew, false, false);

                            ViewBag.success = true;
                        }
                        else
                        {
                            AddErrors(result);
                        }
                    }
                }
            }
            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> ChangePassword(ChangePasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser();
                if (User.IsInRole("Super Admin"))
                {
                    user = await Usermanager.FindByIdAsync(model.Id);
                }
                else
                {
                    user = await Usermanager.GetUserAsync(User);
                }
                if (user == null)
                {
                    return(RedirectToAction("Login"));
                }

                // ChangePasswordAsync changes the user password
                var result = await Usermanager.ChangePasswordAsync(user,
                                                                   model.CurrentPassword, model.NewPassword);

                // The new password did not meet the complexity rules or
                // the current password is incorrect. Add these errors to
                // the ModelState and rerender ChangePassword view
                if (!result.Succeeded)
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                    return(View());
                }

                // Upon successfully changing the password refresh sign-in cookie
                if (!User.IsInRole("Super Admin"))
                {
                    await Signinmanager.RefreshSignInAsync(user);
                }
                return(View("ChangePasswordConfirmation"));
            }

            return(View(model));
        }