Пример #1
0
        /// <summary>
        /// Sends an e-mail with instructions on changing the user e-mail.
        /// </summary>
        /// <param name="viewModel">Represents the view model of the e-mail.</param>
        public async Task SendEmailChangeEmailAsync(EmailEmailChangeViewModel viewModel)
        {
            // Check the web host environment.
            if (_webHostEnvironment.EnvironmentName != "Production")
            {
                // Return.
                return;
            }
            // Define the variables for the e-mail.
            var apiKey      = _configuration.GetSection("Authentication:SendGrid:AppKey").Value;
            var client      = new SendGridClient(apiKey);
            var from        = new EmailAddress(_configuration.GetSection("EmailSender:Email").Value, _configuration.GetSection("EmailSender:Name").Value);
            var to          = new EmailAddress(viewModel.OldEmail, viewModel.OldEmail);
            var subject     = "NetControl4BioMed - Change your e-mail address";
            var htmlContent = await _renderer.RenderPartialToStringAsync("_EmailEmailChangePartial", viewModel);

            var msg = MailHelper.CreateSingleEmail(from, to, subject, string.Empty, htmlContent);
            // Send the e-mail containing the URL.
            await client.SendEmailAsync(msg);
        }
Пример #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            // Get the current user.
            var user = await _userManager.GetUserAsync(User);

            // Check if the user does not exist.
            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 variables to return to the view.
            View = new ViewModel
            {
                IsEmailConfirmed = user.EmailConfirmed
            };
            // 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());
            }
            // Check if the e-mail is different than the current one.
            if (Input.Email != user.Email)
            {
                // Check if an account with the new e-mail address already exists.
                if (await _userManager.FindByEmailAsync(Input.Email) != null)
                {
                    // Add an error to the model.
                    ModelState.AddModelError(string.Empty, "An account with the new e-mail address already exists.");
                    // Return the page.
                    return(Page());
                }
                // Generate an e-mail change code.
                var code = await _userManager.GenerateChangeEmailTokenAsync(user, Input.Email);

                // Create the callback URL to be encoded in the change email.
                var callbackUrl = _linkGenerator.GetUriByPage(HttpContext, "/Identity/ChangeEmail", handler: null, values: new { userId = user.Id, email = Input.Email, code = code });
                // Define a new view model for the e-mail.
                var emailChangeEmailViewModel = new EmailEmailChangeViewModel
                {
                    OldEmail       = user.Email,
                    NewEmail       = Input.Email,
                    Url            = callbackUrl,
                    ApplicationUrl = _linkGenerator.GetUriByPage(HttpContext, "/Index", handler: null, values: null)
                };
                // Send the e-mail change e-mail to the user.
                await _emailSender.SendEmailChangeEmailAsync(emailChangeEmailViewModel);

                // Display a message.
                TempData["StatusMessage"] = $"Success: An e-mail has been sent to your current e-mail address. Please follow the instructions there in order to change the e-mail address associated with the account.";
                // Redirect to page.
                return(RedirectToPage());
            }
            // Display a message.
            TempData["StatusMessage"] = "Success: All details were already up to date.";
            // Redirect to page.
            return(RedirectToPage());
        }