【问题标题】:Why can it be that my ApiKey variable is in null when I try to create a new user using ASP.NET Core Identity?当我尝试使用 ASP.NET Core Identity 创建新用户时,为什么我的 ApiKey 变量为空?
【发布时间】:2020-05-12 03:52:43
【问题描述】:

我正在尝试使用 ASP.NET Core 身份来操作 ASP.NET Core 中的帐户确认和密码恢复,我正在逐步遵循 Microsoft 文档中关于它的内容,但即便如此,我也不'不知道为什么我的 ApiKey 变量在我尝试注册用户时到达 EmailSender 类中的 Execute 方法时为空?

这是我用作指南的 Microsoft 文档链接:

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/accconfirm?view=aspnetcore-3.0&tabs=visual-studio#register-confirm-email-and-reset-password

这是我的 EmailSender 类的内容:

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

        public AuthMessageSenderOptions Options { get; }

        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("Joe@contoso.com", "Joe El Salmón"),
                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);
        }

我已经配置了我的 StartUp 类。接下来,根据 Microsoft 文档指南,我将运行的代码行不是在 CMD 中而是在包管理控制台中:

dotnet user-secrets init


dotnet new webapp -au Individual -uld -o WebPWrecover
cd WebPWrecover
dotnet run


dotnet user-secrets set SendGridUser SendMailAPIKey
dotnet user-secrets set SendGridKey <SendGridKey>

此外,在路径“C:\Users\User\AppData\Roaming\Microsoft\UserSecrets\”中,我看到了带有我的用户和密钥的 JSON 文件。

我真的很想知道我做错了什么,或者是否缺少某些东西。提前致谢。

【问题讨论】:

  • Options.SendGridKeynull
  • 是的,Option.SendGridKey 为空,我正在审核,但找不到在哪里可以设置它的值。
  • @MauricioHernándezCabrera 我编辑并删除了您的 SendGrid 密钥。您可能想要重新生成一个新的。

标签: c# asp.net asp.net-core


【解决方案1】:

嗯,这里的解决办法是把邮件发到

return Execute(Options.SendGridKey, subject, message, email) 

【讨论】:

    猜你喜欢
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2022-07-13
    • 2019-11-28
    相关资源
    最近更新 更多