Exemplo n.º 1
0
        public async Task <ActionResult> ChangePassword(ChangePasswordInputModel model)
        {
            if (ModelState.IsValid)
            {
#if DEBUG
                Debug.WriteLine("Model is valid");
#endif
                var user = await _userManager.FindByNameAsync(model.UserName);

                if (user != null)
                {
#if DEBUG
                    Debug.WriteLine("user is not null");
#endif
                    if (!(await _userManager.IsUsedPasswordAsync(user.UserID, model.Password)))
                    {
#if DEBUG
                        Debug.WriteLine("Password is NOT used.");
#endif

                        var result2 =
                            await _userManager.ChangePasswordAsync(user.UserID, model.OldPassword, model.Password);

                        if (result2.Succeeded)
                        {
#if DEBUG
                            Debug.WriteLine("Password is changed to succeeded");
#endif
                            var resultSetUsedPassword =
                                await
                                _userManager.SetUsedPasswordAsync(user.UserID, model.Password).ConfigureAwait(true);

#if DEBUG
                            Debug.WriteLine("Used password is saved!");
#endif

                            var result3 = await _userManager.SetSigninEndDateAsync(user); //.ConfigureAwait(false);

#if DEBUG
                            if (result3)
                            {
                                Debug.WriteLine("SetSigninEndDateAsync is  well done");
                            }
#endif
                            var result4 = await _userManager.ResetAccessFailedCountAsync(user.UserID);

#if DEBUG
                            if (result4.Succeeded)
                            {
                                Debug.WriteLine("ResetAccessFailedCountAsync is executed.");
                            }
#endif
                            result4 = await _userManager.SetChangePasswordEndDateAsync(user);

#if DEBUG
                            if (result4.Succeeded)
                            {
                                Debug.WriteLine("SetChangePasswordEndDateAsync is executed.");
                            }
#endif

                            result4 = await _userManager.SetPasswordEnabledAsync(user, false);

#if DEBUG
                            if (result4.Succeeded)
                            {
                                Debug.WriteLine("SetPasswordEnabledAsync is executed.");
                            }
#endif

                            return(RedirectToAction("ResetPasswordConfirmation"));
                        }
                        else
                        {
                            // Активизирована ли функция  блокировки учётки в системе
                            if (_userManager.LockoutEnabled)
                            {
#if DEBUG
                                Debug.WriteLine("++++ GetLockoutEnabledAsync is enabled");
#endif
                                // Увелечение счётчика неудачных попыток ввода пароля
                                if (await _userManager.AccessFailedAsync(user.UserID) == IdentityResult.Success)
                                {
#if DEBUG
                                    Debug.WriteLine("~~~ AccessFailedAsync is working!");
#endif
                                    if (await _userManager.IsLockedOutAsync(user.UserID))
                                    {
#if DEBUG
                                        Debug.WriteLine("Account is locked out!");
#endif
                                        //Заблокировать учётку
                                        return(View("Lockout"));
                                    }
                                }
                            }
                            ModelState.AddModelError("", "Ввод старого пароля не верен!");
                        }
                    }
                    else
                    {
#if DEBUG
                        Debug.WriteLine("Password is used!");
#endif
                        ModelState.AddModelError("", "Пароль уже использовался");
                        return(View());
                    }
                }
            }

#if DEBUG
            Debug.WriteLine("Error is occured in ChangePassword");
#endif
            return(View(model));
        }