Ejemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync()
        {
            // We get the current user.
            var user = await _userManager.GetUserAsync(User);

            // We check if the user exists or not.
            if (user == null)
            {
                // Display a message.
                TempData["StatusMessage"] = "Error: An error occured while trying to load the user data. If you are already logged in, please log out and try again.";
                // Redirect to the home page.
                return(RedirectToPage("/Index"));
            }
            // Define the view.
            View = new ViewModel
            {
                HasPassword = await _userManager.HasPasswordAsync(user)
            };
            // Check if the reCaptcha is valid.
            if (!await _reCaptchaChecker.IsValid(Input.ReCaptchaToken))
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "The reCaptcha verification failed.");
                // Return the page.
                return(Page());
            }
            // Check if the provided model is not valid.
            if (!ModelState.IsValid)
            {
                // Add an error to the model.
                ModelState.AddModelError(string.Empty, "An error was encountered. Please check again the input fields.");
                // Return the page.
                return(Page());
            }
            // Try to change the password or set a new one.
            var result = View.HasPassword ? await _userManager.ChangePasswordAsync(user, Input.OldPassword, Input.NewPassword) : await _userManager.AddPasswordAsync(user, Input.NewPassword);

            // Check if the password update wasn't successful.
            if (!result.Succeeded)
            {
                // Go over the encountered errors
                foreach (var error in result.Errors)
                {
                    // and add them to the model
                    ModelState.AddModelError(string.Empty, error.Description);
                }
                // Return the page.
                return(Page());
            }
            // Define a new view model for the e-mail.
            var emailViewModel = new EmailPasswordChangedViewModel
            {
                Email          = user.Email,
                Url            = _linkGenerator.GetUriByPage(HttpContext, "/Account/Index", handler: null, values: null),
                ApplicationUrl = _linkGenerator.GetUriByPage(HttpContext, "/Index", handler: null, values: null)
            };
            // Send the e-mail changed e-mail to the user.
            await _emailSender.SendPasswordChangedEmailAsync(emailViewModel);

            // Re-sign in the user to update the changes.
            await _signInManager.RefreshSignInAsync(user);

            // Display a message.
            TempData["StatusMessage"] = "Success: The password was successfully updated.";
            // Redirect to page.
            return(RedirectToPage());
        }