11namespace EasyConfig . SiteExtension
22{
3+ using System . Collections . Generic ;
34 using Microsoft . AspNetCore . Hosting ;
45 using Microsoft . Azure . KeyVault ;
56 using Microsoft . Azure . Services . AppAuthentication ;
67 using Microsoft . Extensions . Configuration ;
7- using Microsoft . Extensions . Configuration . AzureKeyVault ;
88 using Microsoft . Extensions . Hosting ;
99
1010 public class Program
@@ -20,27 +20,57 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
2020 //Build the config from sources we have
2121 var config = builder . Build ( ) ;
2222
23+ var uriList = new List < string > ( ) ;
24+
2325 // Get the uri for the Vault from configuration
24- var keyVaultUri = config [ "KeyVault:Uri" ] ;
26+ // Try to get a string from configutation
27+ // This will happen when the config looks like:
28+ // {
29+ // "KeyVault":{
30+ // "Uri": "sample.vault.azure.net/"
31+ // }
32+ // }
33+ var uriString = config [ "KeyVault:Uri" ] ;
34+ if ( ! string . IsNullOrWhiteSpace ( uriString ) )
35+ {
36+ uriList . Add ( uriString ) ;
37+ }
38+ // This will happen when the config looks like:
39+ // {
40+ // "KeyVault":{
41+ // "Uri": [
42+ // "sample1.vault.azure.net/",
43+ // "sample2.vault.azure.net/"
44+ // ]
45+ // }
46+ // }
47+ else
48+ {
49+ uriList = config . GetSection ( "KeyVault:Uri" ) . Get < List < string > > ( ) ;
50+ }
51+
2552
2653 // Add KeyVault only if the uri is not empty
27- if ( ! string . IsNullOrWhiteSpace ( keyVaultUri ) )
54+ if ( uriList ? . Count > 0 )
2855 {
29- //Create Managed Service Identity token provider
56+ // Create Managed Service Identity token provider
3057 var azureServiceTokenProvider = new AzureServiceTokenProvider ( ) ;
3158
32- //Create the Key Vault client
59+ // Create the Key Vault client
3360 var keyVaultClient = new KeyVaultClient (
3461 new KeyVaultClient . AuthenticationCallback (
3562 azureServiceTokenProvider . KeyVaultTokenCallback )
3663 ) ;
3764
38- //Add Key Vault to configuration pipeline
39- _ = builder . AddAzureKeyVault (
40- keyVaultUri ,
41- keyVaultClient ,
42- new DefaultKeyVaultSecretManager ( )
43- ) ;
65+ foreach ( var uri in uriList )
66+ {
67+ // Add Key Vault to configuration pipeline
68+ _ = builder . AddAzureKeyVault (
69+ uri ,
70+ keyVaultClient ,
71+ new PrefixKeyVaultSecretManager ( )
72+ ) ;
73+ }
4474 }
4575 }
4676 ) ;
0 commit comments