Ejemplo n.º 1
0
        /// <summary>
        /// Builds and sends registration confirmation email.
        /// </summary>
        /// <param name="emailAddress"></param>
        /// <param name="user"></param>
        /// <param name="pageModel"></param>
        /// <returns></returns>

        public async Task BuildSendRegistrationConfirmation(string emailAddress, ApplicationUser user, PageModel pageModel)
        {
            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
            var callbackUrl = pageModel.Url.Page(
                "/Account/ConfirmEmail",
                pageHandler: null,
                values: new { area = "Identity", userId = user.Id, code = code },
                protocol: pageModel.Request.Scheme);

            var configuration = await _config.GetConfigAsync();

            var prependedConfirmationMsg = !String.IsNullOrEmpty(configuration.RegistrationApprovalMessage)
                ? configuration.RegistrationApprovalMessage : "";

            await _emailSender.SendEmailAsync(
                emailAddress,
                "Confirm your email",
                $"{prependedConfirmationMsg}Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
        }
        public async Task <IActionResult> OnGetAsync(string email)
        {
            if (email == null)
            {
                return(RedirectToPage("/Index"));
            }

            var user = await _userManager.FindByEmailAsync(email);

            if (user == null)
            {
                return(NotFound($"Unable to load user with email '{email}'."));
            }

            Email = email;
            // Display the default confirmation link if SendGrid User/Key values aren't entered in site configuration area of admin
            var configuration = await _config.GetConfigAsync();

            DisplayConfirmAccountLink = !String.IsNullOrEmpty(configuration.SendGridUser) &&
                                        !String.IsNullOrEmpty(configuration.SendGridKey) ? false : true;
            if (DisplayConfirmAccountLink)
            {
                var userId = await _userManager.GetUserIdAsync(user);

                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                EmailConfirmationUrl = Url.Page(
                    "/Account/ConfirmEmail",
                    pageHandler: null,
                    values: new { area = "Identity", userId = userId, code = code },
                    protocol: Request.Scheme);
            }

            return(Page());
        }