Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access User Secrets in ASP.net core 3.0

Tags:

My question is simple. I have an ASP.net core 3.0 app, I added secrets using visualstudio and pasted my secrets into the secret file normally. Then inside my Program.cs, I added a call to addusersecrets as follows:

...
...
.AddUserSecrets<Startup>()

But while calling my secrets like Configuration["Authentication:Secret"] as I used to do when it was in appsettings.json, I get a null value in return.

I went through stackoverflow and tried solutions like changing my addsecrets as follows:

.AddUserSecrets("fds...-...-...askd")

//OR

.AddUserSecrets(typeof(Startup).Assembly, true, reloadOnChange: true)

BUt none of then works.

I wonder if this secret stuff even works on asp.net core, because I don't see any reason my code doesn't work. please if someone gets it, can you tell me a solution ? Thanks.

like image 796
John Code Avatar asked Nov 15 '19 10:11

John Code


People also ask

How do I enable user secrets?

Enable secret storage By default, the inner text of UserSecretsId is a GUID. The inner text is arbitrary, but is unique to the project. In Visual Studio, right-click the project in Solution Explorer, and select Manage User Secrets from the context menu.

Where are .NET user secrets stored?

Your secrets are stored in a JSON file under your user profile. In a Windows machine, they are stored in the %APPDATA%\Microsoft\UserSecrets\<user_secrets_id>\secrets. json file.

How do I open secret json?

In Rider's Solution Explorer, right-click on the ASP.NET Core project -> Tools -> Open project user secrets. This opens the secrets. json file for your project allowing you to add, edit, remove items by hand.

Do secrets override Appsettings?

Microsoft offers a secret manager tool that allows one to store secrets (in plain text) on the local development PC and these secrets are available locally during application development. These secrets overwrite any settings in appsettings. json and appsettings. {environment}.


1 Answers

Be sure to check that you've set the "ASPNETCORE_ENVIRONMENT" variable to "Development", else your app wont add your user secrets.

  • in powershell:

    $Env:ASPNETCORE_ENVIRONMENT = "Development"

  • cmd:

    setx ASPNETCORE_ENVIRONMENT "Development"

like image 79
CodeRed Avatar answered Nov 08 '22 13:11

CodeRed