11namespace EasyConfig . SiteExtension
22{
33 using Microsoft . AspNetCore . Hosting ;
4+ using Microsoft . Azure . KeyVault ;
5+ using Microsoft . Azure . Services . AppAuthentication ;
6+ using Microsoft . Extensions . Configuration ;
7+ using Microsoft . Extensions . Configuration . AzureKeyVault ;
48 using Microsoft . Extensions . Hosting ;
59
610 public class Program
@@ -9,6 +13,36 @@ public class Program
913
1014 public static IHostBuilder CreateHostBuilder ( string [ ] args ) =>
1115 Host . CreateDefaultBuilder ( args )
12- . ConfigureWebHostDefaults ( webBuilder => webBuilder . UseStartup < Startup > ( ) ) ;
16+ . ConfigureWebHostDefaults ( webBuilder => webBuilder . UseStartup < Startup > ( ) )
17+ . ConfigureAppConfiguration (
18+ ( ctx , builder ) =>
19+ {
20+ //Build the config from sources we have
21+ var config = builder . Build ( ) ;
22+
23+ // Get the uri for the Vault from configuration
24+ var keyVaultUri = config [ "KeyVault:Uri" ] ;
25+
26+ // Add KeyVault only if the uri is not empty
27+ if ( ! string . IsNullOrWhiteSpace ( keyVaultUri ) )
28+ {
29+ //Create Managed Service Identity token provider
30+ var azureServiceTokenProvider = new AzureServiceTokenProvider ( ) ;
31+
32+ //Create the Key Vault client
33+ var keyVaultClient = new KeyVaultClient (
34+ new KeyVaultClient . AuthenticationCallback (
35+ azureServiceTokenProvider . KeyVaultTokenCallback )
36+ ) ;
37+
38+ //Add Key Vault to configuration pipeline
39+ _ = builder . AddAzureKeyVault (
40+ keyVaultUri ,
41+ keyVaultClient ,
42+ new DefaultKeyVaultSecretManager ( )
43+ ) ;
44+ }
45+ }
46+ ) ;
1347 }
1448}
0 commit comments