PI06 i PI06-1. Docker definitions for MSSQL and Postgres. Data seeder/generator for countries and people. Entity Framework example with variants for Postgres and MSSQL
This commit is contained in:
8
DefensiveProgramming/Secrets/Demo.cs
Normal file
8
DefensiveProgramming/Secrets/Demo.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Secrets;
|
||||
|
||||
public class Demo
|
||||
{
|
||||
public int Key0 { get; set; }
|
||||
public string? Key1 { get; set; }
|
||||
public string? Key2 { get; set; }
|
||||
}
|
||||
23
DefensiveProgramming/Secrets/Program.cs
Normal file
23
DefensiveProgramming/Secrets/Program.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Secrets;
|
||||
|
||||
var enumerator = Environment.GetEnvironmentVariables().GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
Console.WriteLine($"{enumerator.Key} = {enumerator.Value}");
|
||||
}
|
||||
Console.WriteLine("----------------------------");
|
||||
IConfiguration configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json") //order is important!
|
||||
.AddJsonFile("missing_one.json", optional: true) //
|
||||
.AddEnvironmentVariables() // include Package Microsoft.Extensions.Configuration.EnvironmentVariables
|
||||
.AddUserSecrets("PI-Secrets") //package Microsoft.Extensions.Configuration.UserSecrets
|
||||
.Build();
|
||||
|
||||
Console.WriteLine($"Custom env variable using configuration = " + configuration["CustomEnvValue"]);
|
||||
Console.WriteLine($"Name = " + configuration["Name"]);
|
||||
Console.WriteLine($"Key0 = " + configuration["Demo:Key0"]); //or Demo__Key0
|
||||
|
||||
Demo? demo = configuration.GetSection("Demo").Get<Demo>(); // package Microsoft.Extensions.Configuration.Binder
|
||||
Console.WriteLine($"Key1 = " + demo?.Key1);
|
||||
Console.WriteLine($"Key2 = " + demo?.Key2);
|
||||
10
DefensiveProgramming/Secrets/Properties/launchSettings.json
Normal file
10
DefensiveProgramming/Secrets/Properties/launchSettings.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Secrets": {
|
||||
"commandName": "Project",
|
||||
"environmentVariables": {
|
||||
"CustomEnvValue": "Demo"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
23
DefensiveProgramming/Secrets/Secrets.csproj
Normal file
23
DefensiveProgramming/Secrets/Secrets.csproj
Normal file
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<UserSecretsId>PI</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.6" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="10.0.6" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="10.0.6" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
7
DefensiveProgramming/Secrets/appsettings.json
Normal file
7
DefensiveProgramming/Secrets/appsettings.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"Name": "Bob",
|
||||
"Demo": {
|
||||
"Key0": 123,
|
||||
"Key1": "something that should not be publicly available"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user