I have winform application written in c#. I am trying to fetch azure secrets from Azure KeyVault. I have written below function to fetch the secret.
private async Task<string> GetSecretFromAzureKV(string secretName)
{
try
{
var keyVaultName = "my-Keyvault-name";
var kvUri = $"https://{keyVaultName}.vault.azure.net";
var credentials = new DefaultAzureCredential();
var client = new SecretClient(new Uri(kvUri), credentials);
var secret = await client.GetSecretAsync(secretName);
return secret.Value.Value;
}
catch (Exception ex)
{
throw;
}
}
When I debug this solution I can see below error.
Also, note that I am not running it on Azure VM. The code is on my local machine.
Any help is greatly appreciated. Thanks.
2
Answers
Not sure how but replacing async method to sync worked for me. Thanks @Harshita for the help.
In Azure Key Vault, check whether you have the below configurations in
Access configuration
.If you have
Vault access policy
you will be able to add the secrets and retrieve them without any issues.OR
If you have
Azure role-based access control
, then you must have a Key Vault related role (KeyVault Administrator
orKeyVault Secret User
) assigned to service principle or user.Access policies
, make sure you have access policy with your ID/Name and haveGet/List
Key/Secret permissions.Those are the default messages which will be shown in the
DefaultAzurecredential
.