Reputation: 288
var keyVaultClient = new KeyVaultClient(
new KeyVaultClient.AuthenticationCallback(
azureServiceTokenProvider.KeyVaultTokenCallback));
var secret = await keyVaultClient
.GetSecretAsync("https://KeyvaultName.vault.azure.net/secrets/test1")
.ConfigureAwait(false);
ViewData["keyvaultName"] = secret.Value;
//It is working fine. But now i want to get all the secrets in a single call and bind it to the fields
Upvotes: 0
Views: 1813
Reputation: 288
But this code will not run in local, i.e, while development. For this we need to install Azure CLI (azure-cli-2.0.29.msi) to make use of MSI in local environment. After installing this open Microsoft azure command prompt and run "az login" command and open the url mentioned in the command prompt and copy the code mentioned in prompt in that url. Now you would be able to make use of key vault using MSI in local and app service as well.
Dictionary<string, string> secretlist = new Dictionary<string, string>();
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
// TO get access token to azureServices
Task<string> accessToken = azureServiceTokenProvider.GetAccessTokenAsync("https://vault.azure.net");
accessToken.Wait();
var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var all = keyVaultClient.GetSecretsAsync("https://keyvaultName.vault.azure.net/");
string seperator = "secrets/";
foreach (Microsoft.Azure.KeyVault.Models.SecretItem someItem in all.Result)
{
var secretName = someItem.Identifier;
var secretValue = keyVaultClient.GetSecretAsync(secretName.ToString());
secretValue.Wait();
secretlist.Add(secretName.ToString().Substring(secretName.ToString().IndexOf(seperator) + seperator.Length), secretValue.Result.Value);
}
Upvotes: 1
Reputation: 1982
Key Vault only supports retrieving a single secret value at a time.
Upvotes: 2