예제 #1
0
        public async Task <IActionResult> Index(string email)
        {
            var issuer        = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase.Value}/";
            var audience      = _appSettings.B2CClientId;
            var tokenDuration = _appSettings.LinkExpiresAfterMinutes;

            // Build/compute the token that is used as an assertion value as part of the OIDC request to the B2C endpoint
            var token = await _keyVaultCertificateHelper
                        .BuildSerializedIdTokenAsync(issuer, audience, tokenDuration, email)
                        .ConfigureAwait(false);

            string link = BuildUrl(token);

            // Read the HTML email template from the local file provided by and deployed with the project
            // TODO - move the actual reading of the file to a singleton/DI
            var emailHtmlTemplate = await System.IO.File
                                    .ReadAllTextAsync(Path.Combine(_hostingEnvironment.ContentRootPath, "Template.html"))
                                    .ConfigureAwait(false);

            emailHtmlTemplate = emailHtmlTemplate.Replace("{0}", email);
            emailHtmlTemplate = emailHtmlTemplate.Replace("{1}", link);

            // Send the mail
            await _mailSender
            .SendEmailAsync(_appSettings.SMTPFromAddress, email, _appSettings.SMTPSubject, emailHtmlTemplate)
            .ConfigureAwait(false);

            ViewData["Message"] = $"Email sent to {email}";

            return(View());
        }
예제 #2
0
        public async Task <IActionResult> Send(string email, string state)
        {
            var issuer        = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase.Value}/";
            var audience      = _appSettings.B2CClientId;
            var tokenDuration = _appSettings.LinkExpiresAfterMinutes;

            // Build/compute the token that is used as an assertion value as part of the OIDC request to the B2C endpoint
            var token = await _keyVaultCertificateHelper
                        .BuildSerializedIdTokenAsync(issuer, audience, tokenDuration, email)
                        .ConfigureAwait(false);

            string link = $"{_appSettings.B2CRedirectUri}?id_token_hint={token}&state={state}";
            var    emailHtmlTemplate = $"<h1>Welcome {email}</h1> In order to login please click on <a href='{link}'>this link</a><br/>Thanks";

            // Send the mail
            await _mailSender
            .SendEmailAsync(_appSettings.SMTPFromAddress, email, _appSettings.SMTPSubject, emailHtmlTemplate)
            .ConfigureAwait(false);

            _logger.LogDebug("Sent email to {0} with state: {1}", email, state);

            return(Content($"<h1>Email Sent!</h1> (but for debug click this <a href='{link}' target='_blank'>link</a>)", "text/html"));
        }