簡體   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