dotnet user secrets not working
So basically, I am using Docker and I am trying to make it so that for development I can use
user secrets
to store things like ClientId
, ClientSecrets
and DatabaseConnection
instead of using Appsettings.json
. so I store all the things I need for development in secrets.json
And then in Program.cs`, it should read from the user secrets instead of
Appsettings.json, i actually deleted appsettings.json. Here is what I did:
```dotnet user-secrets set "GoogleKeys:ClientSecret" "123123"
dotnet user-secrets set "GoogleKeys:ClientId" "456456"
dotnet user-secrets set "ConnectionStrings:DatabaseConnection" "Host=<host>;Port=5432;Database=<database>;Username=<username>;Password=<password>"
```
so now I see them when i do
dotnet user-secrets list:
```
GoogleKeys:ClientSecret = 123123
GoogleKeys:ClientId = 456456
ConnectionStrings:DatabaseConnection = Host=<host>;Port=<port>;Database=<database>;Username=<username>;Password=<password>```
I dont see them in secrets.json for some reason but I guess thats okay cause the command returned them. I also know the secrets.json is hooked up cause when i go to the
.csproj``, :
the value in here matches the value in my local directory. there is an image attached that shows that they matched.5 Replies
So now in my
Program.cs
, I have to have it read from the user secrets instead of appsettings.json:
the problem when this part is that, I am unable to access any of my usersecrets.
For example: var mySecret = builder.Configuration["ConnectionStrings:DatabaseConnection"];
, mySecret is always null. Its not in the builder Configuration but I dont understand why if I added it to my user secrets.
I also tried builder.Configuration.AddUserSecrets<Program>();
, but this didnt work either. So of course because it cant get the clientId, it causes the entire app to crash. Nothing I have tried worked I am unable to get the user secrets to work in Program.cs
. Is it because they should only be used in controllers?Is the app running in a docker container (Since you mentioned Docker)?
In that case I had some issues with user secrets mounting
yea its running with docker compose
https://stackoverflow.com/questions/67569879/user-secrets-not-being-read-fileprovider-is-showing-projects-debug-path/71777968#71777968
I wrote an answer on SO for a similar problem once
Maybe that's the issue you also run into
But the answer is from Net6 still, but worth a shot I guess
thx il check it out