简体   繁体   中英

Why can it be that my ApiKey variable is in null when I try to create a new user using ASP.NET Core Identity?

I'm trying to use the ASP.NET Core Identity to manipulate the Account confirmation and password recovery in ASP.NET Core, I'm following step by step what the Microsoft docs about it says, but even so, I don't know why my ApiKey variable is null when it gets to Execute method in EmailSender class when I try to register a user?

Here is the Microsoft docs link what I'm using as guide:

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

This is the content of my EmailSender class:

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);
        }

I already configured my StartUp class. Next, I put the codelines I run not in CMD but in Package Administration Console, according to the Microsoft docs guide:

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>

Also, in the route "C:\Users\User\AppData\Roaming\Microsoft\UserSecrets\" I see the JSON file with my User and Key.

I really want to know what I'm doing wrong or if something is missing. Thanks in advance.

Well, the solution here is to put the email to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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