C
C#14mo ago
Pingu

✅ Reading Secret.json

Hi, I want to read my secrets.json file, I set it but it doesnt work my secrets.json:
{
"Platanka:SMTPMail": "[email protected]",
"Platanka:MailPassword": "password"
"Platanka:dbConnectionString": "stringconnect"
}
{
"Platanka:SMTPMail": "[email protected]",
"Platanka:MailPassword": "password"
"Platanka:dbConnectionString": "stringconnect"
}
No description
31 Replies
3R1C
3R1C14mo ago
"Plantaka":
{
"SMTPMail": "[email protected]",
"MailPassword": "password",
"dbConnectionString": "stringconnect"
}
"Plantaka":
{
"SMTPMail": "[email protected]",
"MailPassword": "password",
"dbConnectionString": "stringconnect"
}
Pingu
PinguOP14mo ago
still doest work ;/
No description
No description
3R1C
3R1C14mo ago
What error is it giving you? Seeing as you mention "secrets.json" did you add it?
config.AddJsonFile("secrets.json", optional: false);
config.AddJsonFile("secrets.json", optional: false);
Guess my question should be, how are you interacting with the json ?
Pingu
PinguOP14mo ago
i didnt add
config.AddJsonFile("secrets.json", optional: false);
config.AddJsonFile("secrets.json", optional: false);
@3R1C but when I added give me return:
Unhandled exception. System.IO.FileNotFoundException: The configuration file 'secrets.json' was not found and is not optional. The expected physical path was '/app/secrets.json'.
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.ConfigurationManager.AddSource(IConfigurationSource source)
at Microsoft.Extensions.Configuration.ConfigurationManager.Microsoft.Extensions.Configuration.IConfigurationBuilder.Add(IConfigurationSource source)
at Program.<Main>$(String[] args) in /src/PlatankaAPI/Program.cs:line 14
Unhandled exception. System.IO.FileNotFoundException: The configuration file 'secrets.json' was not found and is not optional. The expected physical path was '/app/secrets.json'.
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.ConfigurationManager.AddSource(IConfigurationSource source)
at Microsoft.Extensions.Configuration.ConfigurationManager.Microsoft.Extensions.Configuration.IConfigurationBuilder.Add(IConfigurationSource source)
at Program.<Main>$(String[] args) in /src/PlatankaAPI/Program.cs:line 14
No description
Pingu
PinguOP14mo ago
I am using .net webapi and secrets manager secrets file is located in .microsoft/usersecrets not in project folder
Pobiega
Pobiega14mo ago
Pingu
PinguOP14mo ago
I already used this and I still dont know What I am meant to do
Pobiega
Pobiega14mo ago
is your secret identifier in your csproj?
Pingu
PinguOP14mo ago
I have my <UserSecretsId>id</UserSecretsId> in csproj when i enter in cmd dotnet user-secrets list shows me my secrets
Pobiega
Pobiega14mo ago
okay good can you show me the content of your secrets file, verbatim? ie exactly like it is
Pingu
PinguOP14mo ago
here After :
dotnet user-secrets list
dotnet user-secrets list
i got Plantaka:SMTPMail = mail...
Pobiega
Pobiega14mo ago
give me a sec to reproduce
Pobiega
Pobiega14mo ago
No description
Pobiega
Pobiega14mo ago
works fine here. this is a brand new .net 7 webapp with dotnet user-secrets init and dotnet user-secrets set "Platanka:connStr" "1234"
PS D:\Code\WebApplicationSecrets\WebApplicationSecrets> dotnet user-secrets init
Set UserSecretsId to '350765fc-997b-47d8-9930-e6a4736ab9f4' for MSBuild project 'D:\Code\WebApplicationSecrets\WebApplicationSecrets\WebApplicationSecrets.csproj'.
PS D:\Code\WebApplicationSecrets\WebApplicationSecrets> dotnet user-secrets list
No secrets configured for this application.
PS D:\Code\WebApplicationSecrets\WebApplicationSecrets> dotnet user-secrets set "Platanka:connStr" "1234"
Successfully saved Platanka:connStr = 1234 to the secret store.
PS D:\Code\WebApplicationSecrets\WebApplicationSecrets> dotnet user-secrets list
Platanka:connStr = 1234
PS D:\Code\WebApplicationSecrets\WebApplicationSecrets>
PS D:\Code\WebApplicationSecrets\WebApplicationSecrets> dotnet user-secrets init
Set UserSecretsId to '350765fc-997b-47d8-9930-e6a4736ab9f4' for MSBuild project 'D:\Code\WebApplicationSecrets\WebApplicationSecrets\WebApplicationSecrets.csproj'.
PS D:\Code\WebApplicationSecrets\WebApplicationSecrets> dotnet user-secrets list
No secrets configured for this application.
PS D:\Code\WebApplicationSecrets\WebApplicationSecrets> dotnet user-secrets set "Platanka:connStr" "1234"
Successfully saved Platanka:connStr = 1234 to the secret store.
PS D:\Code\WebApplicationSecrets\WebApplicationSecrets> dotnet user-secrets list
Platanka:connStr = 1234
PS D:\Code\WebApplicationSecrets\WebApplicationSecrets>
Pingu
PinguOP14mo ago
mhm I used
dotnet user-secrets set "Platanka:connStr" "1234"
dotnet user-secrets set "Platanka:connStr" "1234"
and change value, and in Docker compose my WebApi returns:
Connection id "0HMU1RT1URH9C", Request id "0HMU1RT1URH9C:00000003": An unhandled exception was thrown by the application.
System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
Connection id "0HMU1RT1URH9C", Request id "0HMU1RT1URH9C:00000003": An unhandled exception was thrown by the application.
System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
Pobiega
Pobiega14mo ago
.. docker compose?
Pingu
PinguOP14mo ago
yes
Pobiega
Pobiega14mo ago
yeah no shit it doesnt work with docker compose my dude this is literally only for local development
Pingu
PinguOP14mo ago
hmm , How can i put secrets in compose?
Pobiega
Pobiega14mo ago
env vars Platanka__connStr would be the name you'd give it to make it work for that path note the double underscore
Pingu
PinguOP14mo ago
Okey thank you , I need try it
Pobiega
Pobiega14mo ago
var messageFromEnv = builder.Configuration["Platanka:env"];

Console.WriteLine(messageFromEnv);
Console.WriteLine();
var messageFromEnv = builder.Configuration["Platanka:env"];

Console.WriteLine(messageFromEnv);
Console.WriteLine();
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5251",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"Platanka__env": "this is from an env var"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5251",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"Platanka__env": "this is from an env var"
}
},
this works fine
Pingu
PinguOP14mo ago
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"Platanka__env": "mongodb://host.docker.internal:27017/"
},
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"Platanka__env": "mongodb://host.docker.internal:27017/"
},
var messageFromEnv = builder.Configuration["Platanka:env"];
var messageFromEnv = builder.Configuration["Platanka:env"];
still doesnt work
Pobiega
Pobiega14mo ago
are you trying via docker compose again? you need to set the env vars inside the compose file in that case
Pingu
PinguOP14mo ago
iam using docker compose up --build yea probably
Pobiega
Pobiega14mo ago
not yea probably. thats exactly what you must do
Pingu
PinguOP14mo ago
Okey, thank you for your time Now is working. Thank you again for gaining new experiences! I used in docker-compose.yml file:
platankaapi:
env_file:
- .env
platankaapi:
env_file:
- .env
.env:
DBCONNECTINGSTRING=mongodb://host.docker.internal:27017/
DBCONNECTINGSTRING=mongodb://host.docker.internal:27017/
program.cs:
string dbString = Environment.GetEnvironmentVariable("DBCONNECTINGSTRING");
string dbString = Environment.GetEnvironmentVariable("DBCONNECTINGSTRING");
and its Works
Pobiega
Pobiega14mo ago
yes thats how you do secrets in non-dev environments user-secrets is specifically for local dev, aka via visual studioo
Pingu
PinguOP14mo ago
Alright
Pobiega
Pobiega14mo ago
(or dotnet run or via rider etc) $close
MODiX
MODiX14mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server