예제 #1
0
        public async Task <IActionResult> ChangePasswordFormAction(PersonalDataModel model)
        {
            UserInfo uInfo = await _userInfoManager.GetUserInfoAsync(User.Identity.Name);

            model.Address       = uInfo.Address;
            model.BirthDay      = uInfo.BirthDay;
            model.BirthMonth    = uInfo.BirthMonth;
            model.BirthMonthStr = uInfo.BirthMonthStr;
            model.BirthYear     = uInfo.BirthYear;
            model.Email         = uInfo.Email;
            model.FirstName     = uInfo.FirstName;
            model.Gender        = uInfo.Gender;
            model.LastName      = uInfo.LastName;
            model.Login         = uInfo.Login;
            model.Phone         = uInfo.Phone;

            if (!await _moneyUserManager.IsNotOAuth(User.Identity.Name))
            {
                ViewData["PassChangeClass"]   = "text-danger";
                ViewData["PassChangeMessage"] = "Password change failed: authentication type error.";
                return(View(nameof(MainController.PersonalData), model));
            }

            if (!await _authentication.CheckPasswordAsync(model.ChangePassword.CurrentPassword, User.Identity.Name))
            {
                ViewData["PassChangeClass"]   = "text-danger";
                ViewData["PassChangeMessage"] = "Password change failed: wrong current password.";
                return(View(nameof(MainController.PersonalData), model));
            }

            IdentityResult result = await _moneyUserManager.ChangePasswordAsync(model.ChangePassword.CurrentPassword, model.ChangePassword.Password, User.Identity.Name);

            if (!result.Succeeded)
            {
                ViewData["PassChangeClass"]   = "text-danger";
                ViewData["PassChangeMessage"] = "Password change failed.";
                return(View(nameof(MainController.PersonalData), model));
            }

            await _logManager.WriteAsync(User.Identity.Name, $"User '{User.Identity.Name}' changed his password.");

            ViewData["PassChangeClass"]   = "text-success";
            ViewData["PassChangeMessage"] = "Password successfully changed.";
            return(View(nameof(MainController.PersonalData), model));
        }