C
C#4mo ago
Ares

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``, :
<UserSecretsId>02aa0af3-c478-4f3a-addb-31dce40f9f96</UserSecretsId>
<UserSecretsId>02aa0af3-c478-4f3a-addb-31dce40f9f96</UserSecretsId>
the value in here matches the value in my local directory. there is an image attached that shows that they matched.
No description
5 Replies
Ares
Ares4mo ago
So now in my Program.cs, I have to have it read from the user secrets instead of appsettings.json:
var builder = WebApplication.CreateBuilder(args);

builder
.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogle(
GoogleDefaults.AuthenticationScheme,
options =>
{
options.ClientId = builder.Configuration.GetSection("GoogleKeys:ClientId").Value;
options.ClientSecret = builder
.Configuration.GetSection("GoogleKeys:ClientSecret")
.Value;
options.ClaimActions.MapJsonKey("urn:google:picture", "picture", "url");
options.ClaimActions.MapJsonKey("urn:google:locale", "locale", "string");
}
);
var builder = WebApplication.CreateBuilder(args);

builder
.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogle(
GoogleDefaults.AuthenticationScheme,
options =>
{
options.ClientId = builder.Configuration.GetSection("GoogleKeys:ClientId").Value;
options.ClientSecret = builder
.Configuration.GetSection("GoogleKeys:ClientSecret")
.Value;
options.ClaimActions.MapJsonKey("urn:google:picture", "picture", "url");
options.ClaimActions.MapJsonKey("urn:google:locale", "locale", "string");
}
);
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?
Sossenbinder
Sossenbinder4mo ago
Is the app running in a docker container (Since you mentioned Docker)? In that case I had some issues with user secrets mounting
Ares
Ares4mo ago
yea its running with docker compose
Sossenbinder
Sossenbinder4mo ago
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
Ares
Ares4mo ago
thx il check it out