C
C#9mo 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": "mail@gmail.com",
"Platanka:MailPassword": "password"
"Platanka:dbConnectionString": "stringconnect"
}
{
"Platanka:SMTPMail": "mail@gmail.com",
"Platanka:MailPassword": "password"
"Platanka:dbConnectionString": "stringconnect"
}
No description
31 Replies
3R1C
3R1C9mo ago
"Plantaka":
{
"SMTPMail": "mail@gmail.com",
"MailPassword": "password",
"dbConnectionString": "stringconnect"
}
"Plantaka":
{
"SMTPMail": "mail@gmail.com",
"MailPassword": "password",
"dbConnectionString": "stringconnect"
}
Pingu
Pingu9mo ago
still doest work ;/
No description
No description
3R1C
3R1C9mo 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
Pingu9mo 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
Pingu9mo ago
I am using .net webapi and secrets manager secrets file is located in .microsoft/usersecrets not in project folder
Pobiega
Pobiega9mo ago
Pingu
Pingu9mo ago
I already used this and I still dont know What I am meant to do
Pobiega
Pobiega9mo ago
is your secret identifier in your csproj?
Pingu
Pingu9mo ago
I have my <UserSecretsId>id</UserSecretsId> in csproj when i enter in cmd dotnet user-secrets list shows me my secrets
Pobiega
Pobiega9mo ago
okay good can you show me the content of your secrets file, verbatim? ie exactly like it is
Pingu
Pingu9mo ago
here After :
dotnet user-secrets list
dotnet user-secrets list
i got Plantaka:SMTPMail = mail...
Pobiega
Pobiega9mo ago
give me a sec to reproduce
Pobiega
Pobiega9mo ago
No description
Pobiega
Pobiega9mo 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
Pingu9mo 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
Pobiega9mo ago
.. docker compose?
Pingu
Pingu9mo ago
yes
Pobiega
Pobiega9mo ago
yeah no shit it doesnt work with docker compose my dude this is literally only for local development
Pingu
Pingu9mo ago
hmm , How can i put secrets in compose?
Pobiega
Pobiega9mo 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
Pingu9mo ago
Okey thank you , I need try it
Pobiega
Pobiega9mo 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
Pingu9mo 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
Pobiega9mo ago
are you trying via docker compose again? you need to set the env vars inside the compose file in that case
Pingu
Pingu9mo ago
iam using docker compose up --build yea probably
Pobiega
Pobiega9mo ago
not yea probably. thats exactly what you must do
Pingu
Pingu9mo 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
Pobiega9mo ago
yes thats how you do secrets in non-dev environments user-secrets is specifically for local dev, aka via visual studioo
Pingu
Pingu9mo ago
Alright
Pobiega
Pobiega9mo ago
(or dotnet run or via rider etc) $close
MODiX
MODiX9mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server
More Posts
❔ Converting code to another LanguageSo for an engineering/ cs project, we’re making a satellite (lookup CanSat if you want more details)✅ Questions about castingHi all, I'm looking to simplify some network code I've got. Networking doesn't really play too large❔ c# wpf change background color of whitespace characterI've been at it for a few hours and i just cant seem to get it to work, i've tried keypress events a❔ .NET installation not recognized, but command worksI am trying to install .NET Core 7. The installation goes smoothly and I can run the command, but li❔ Need a hand on creating something i am doing on fiddler which involve web traffic editingI've never done anything dealing with web traffic programmically so this is somewhat new to me but h❔ Why is Pop() necessary here??So I found a sample solution for the LeetCode decode string puzzle https://leetcode.com/problems/decI don't really get how to create new methods for my code. Yesterday people told me it's got no body.Didn't really understand anything when i did research. It's probably a quick fix : ] I'm talking ab❔ MC3050 compiler errorI added a new project with some custom WPF controls to my project's dependencies. I then added the ✅ Function For Division Game Showing Same Number Twice???```cs public void StartDivGame(string username, DateTime date, int totalQuestions, string difficultygoto vs recursionHello, I have a little question about "good habit" in this block of codes. So a little context is, t