.net core 3.1 call to User Secrets for SendGrid keys returns null values

872 Views Asked by At

I have set up a project following https://learn.microsoft.com/en-us/aspnet/core/security/authentication/accconfirm?view=aspnetcore-3.1&tabs=visual-studio

From Startup.cs:

            services.AddTransient<IEmailSender, EmailSender>();
            services.Configure<AuthMessageSenderOptions>(Configuration);
            _secretOne = Configuration["SecretStuff:SecretOne"];

the _secretOne variable was added to prove that the correct secrets.json file is being accessed. It has both a SecretStuff block and a AuthMessageSenderOptions block.

In EmailSender.cs

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

A breakpoint after Options = shows the keys with null values.

Eventually I gave up and hard coded the Options.SendGridKey and Options.SendGridUser and with this change the project works as it should.

This is my first use of User-Secrets so when it did not work I set up a console app that references the same secrets.json file and it sends emails.

1

There are 1 best solutions below

0
On

I found a possible answer in: https://www.twilio.com/blog/2018/05/user-secrets-in-a-net-core-web-app.html

I changed Startup.cs to:

services.Configure<AuthMessageSenderOptions>(Configuration.GetSection("AuthMessageSenderOptions"));

and now the values are as they should be. I sure wasted a lot of time looking in the wrong places for an answer.