Skip to content

Instantly share code, notes, and snippets.

@MahdiKarimipour
Last active September 28, 2021 10:59
Show Gist options
  • Save MahdiKarimipour/a81a66ec5fdac749a8cadc40e9963275 to your computer and use it in GitHub Desktop.
Save MahdiKarimipour/a81a66ec5fdac749a8cadc40e9963275 to your computer and use it in GitHub Desktop.
Read Secrets at Start up Time for Asp.NET APIs
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