繁体   English   中英

Asp.Net Core Identity SendGrid 邮件未发送

[英]Asp.Net Core Identity SendGrid mails are not being sent

我试图找出为什么在创建新用户帐户时没有从我的应用程序发送帐户验证 email。

在我的第一次尝试中,我能够发送两封电子邮件。 这些电子邮件最终进入了我的垃圾邮件过滤器,但它们确实通过了。 我不知道从那以后可能发生了什么变化。

检查 SendGrid 控制面板,我可以确认在我尝试的第一天发送了两封电子邮件,但我后来的尝试都没有生成任何电子邮件。

本文建议在EmailSender.Execute上设置断点。 我这样做了,发现它确实没有被击中。 如何进一步调试?

SendGrid 账户信息在 secrets.json 中指定:

{
  "SendGridUser": "{account name}",
  "SendGridKey": "{api-key}"
}

在 Startup.cs 中配置了一个服务:

services.AddTransient<IEmailSender, EmailSender>();
services.Configure<AuthMessageSenderOptions>(Configuration);

AuthMessageSender 选项:

public class AuthMessageSenderOptions
{
    public string SendGridUser { get; set; }
    public string SendGridKey { get; set; }
}

EmailSender 服务:

public class EmailSender : IEmailSender
{
    public EmailSender(IOptions<AuthMessageSenderOptions> optionsAccessor)
    {
        Options = optionsAccessor.Value;
    }

    public AuthMessageSenderOptions Options { get; } //set only via Secret Manager

    public Task SendEmailAsync(string email, string subject, string message)
    {
        return Execute(Options.SendGridKey, subject, message, email);
    }

    public Task Execute(string apiKey, string subject, string message, string email)
    {
        var client = new SendGridClient(apiKey);
        var msg = new SendGridMessage()
        {
            From = new EmailAddress("my@email.com", Options.SendGridUser),
            Subject = subject,
            PlainTextContent = message,
            HtmlContent = message
        };
        msg.AddTo(new EmailAddress(email));

        // Disable click tracking.
        // See https://sendgrid.com/docs/User_Guide/Settings/tracking.html
        msg.SetClickTracking(false, false);

        return client.SendEmailAsync(msg);
    }
}

一个用户是这样创建的:

// user is an ApplicationUser-object
IdentityResult result = await userManager.CreateAsync(auto.Map<ApplicationUser>(user), "P&55w0rd");
if (result.Succeeded)
{
    ApplicationUser u = await userManager.FindByNameAsync(user.UserName);
    if (u != null)
    { 
        // newUser.RN is the role name to be assigned to the new user
        await userManager.AddToRoleAsync(u, newUser.RN);
    }
    return RedirectToAction("Details", new { id = user.Id });
}
else
{
    foreach (var error in result.Errors)
    {
        ModelState.AddModelError("", error.Description);
    }
}

创建了一个新用户,添加到该角色,我们被重定向到 Users/Details/{id},但没有发送帐户验证 email。

如果 Identity 在默认情况下仅通过设置 EmailSender 就可以做到这一点,我会感到惊讶。 您似乎没有为确认提供任何逻辑,也无处可调用 EmailSender。

您需要在创建用户的IEmailSender作为服务注入,并添加生成确认令牌的逻辑并实际发送 email。

我期望以下内容:

var token = await userManager.GenerateEmailConfirmationTokenAsync(user);
var confirmationLink = Url.Action(nameof(ConfirmEmail), "Account", 
   new { token , email = user.Email }, 
   Request.Scheme);
await _emailSender.SendEmailAsync(user.Email, "Confirmation email link", confirmationLink);

当然,您可以进一步了解如何使您的 email 更漂亮,但这是它的核心。

此外,为了确保您了解全貌,Identity 默认不提供 email 实现,您还必须对其进行设置: https://docs.microsoft.com/en-us/aspnet/core/安全/身份验证/accconfirm?view=aspnetcore-3.1&tabs=visual-studio#install-sendgrid

如果发件人的 email 未正确验证,Sendgrid 不会发送电子邮件。 希望这对某人有帮助。 文档: https://app.sendgrid.com/settings/sender_auth

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM