vikas biradar
vikas biradar

Reputation: 288

i want to call all the secret from the key vault in a single call. is there any way to do that?

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

Answers (2)

vikas biradar
vikas biradar

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

Rich Randall
Rich Randall

Reputation: 1982

Key Vault only supports retrieving a single secret value at a time.

Upvotes: 2

Related Questions