Exemplo n.º 1
0
        public async Task <IActionResult> ChangePasswordAsync(ChangePasswordViewModel model)
        {
            if (model.OldPassword == model.NewPassword)
            {
                ViewData[ErrorKey] = string.Format(ObjectsShouldNotMatchErrorMessage,
                                                   model.GetPropertyDisplayName(nameof(model.OldPassword)),
                                                   model.GetPropertyDisplayName(nameof(model.NewPassword)).ToLower());
                return(View(model));
            }
            User profile = await profileService.GetUserAsync(User);

            var changePasswordResult = await profileService
                                       .ChangePasswordAsync(profile, model.OldPassword, model.NewPassword);

            if (!changePasswordResult.Succeeded)
            {
                ViewData[ErrorKey] = string.Format(InvalidObjectErrorMessage,
                                                   model.GetPropertyDisplayName(nameof(model.OldPassword)));
                return(View(model));
            }
            await signInService.RefreshSignInAsync(profile);

            logger.LogInformation($"'{profile.UserName}' changed their password.");
            string profileViewUrl = Url.Action(
                action: nameof(Profile),
                controller: this.ControllerName(),
                values: new { pid = profile.Id },
                protocol: Request.Scheme);

            return(Redirect(profileViewUrl));
        }