Last active
September 28, 2021 10:59
-
-
Save MahdiKarimipour/a81a66ec5fdac749a8cadc40e9963275 to your computer and use it in GitHub Desktop.
Read Secrets at Start up Time for Asp.NET APIs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Host.CreateDefaultBuilder(args) | |
.ConfigureWebHostDefaults(webBuilder => | |
{ | |
webBuilder.UseStartup<Startup>(); | |
}) | |
.ConfigureAppConfiguration((context, config) => | |
{ | |
var builtConfig = config.SetBasePath(Directory.GetCurrentDirectory()) | |
.AddJsonFile("appsettings.json", false) | |
.AddJsonFile($"appsettings.Production.json", true) | |
.AddEnvironmentVariables() | |
.Build(); | |
var settings = builtConfig.Get<AppSettings>(); | |
if (settings.ContainerMode) | |
{ | |
if (context.HostingEnvironment.IsProduction()) | |
{ | |
//1: Add plain secrets (no configs) as Opaque secrets | |
//2: Secrets are accessible through separate files | |
//3: Read the secret map values CSI volume of KeyVault provider | |
config.AddKeyPerFile($"/root/.microsoft/usersecrets"); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment