Skip to content

Instantly share code, notes, and snippets.

@wvanbesien
Created June 14, 2020 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wvanbesien/245b44a7f9ad7edbb36a3acba4fc1aa5 to your computer and use it in GitHub Desktop.
Save wvanbesien/245b44a7f9ad7edbb36a3acba4fc1aa5 to your computer and use it in GitHub Desktop.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((webBuilder) => {
var config = webBuilder.Build();
var mode = (KeyVaultUsage)Enum.Parse(typeof(KeyVaultUsage), (config["Secrets:Mode"]));
if (mode != KeyVaultUsage.UseLocalSecretStore)
{
KeyVaultOptions kvc = config.GetSection("Secrets").Get<KeyVaultOptions>();
if (mode == KeyVaultUsage.UseClientSecret)
{
webBuilder.AddAzureKeyVault(kvc.KeyVaultUri, kvc.ClientId, kvc.ClientSecret);
}
else //UseMsi
{
var tokenProvider = new AzureServiceTokenProvider();
//Create the Key Vault client
var kvClient = new KeyVaultClient((authority, resource, scope) => tokenProvider.KeyVaultTokenCallback(authority, resource, scope));
//Add Key Vault to configuration pipeline
webBuilder.AddAzureKeyVault(kvc.KeyVaultUri, kvClient, new DefaultKeyVaultSecretManager());
}
}
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment