skip to Main Content

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.
enter image description here

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


  1. Chosen as BEST ANSWER

    Not sure how but replacing async method to sync worked for me. Thanks @Harshita for the help.

    enter image description here


  2. 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 or KeyVault Secret User) assigned to service principle or user.

    enter image description here

    • And in Access policies, make sure you have access policy with your ID/Name and have Get/List Key/Secret permissions.

    enter image description here

    • Also check that your Key Vault Secret is Enabled.

    enter image description here

    error on getting Azure secret from KeyVault "DefaultAzureCredential failed to retrieve a token from the included credentials."

    Those are the default messages which will be shown in the DefaultAzurecredential.

    enter image description here

    • I am able to retrieve the Secret successfully even though I am getting the warnings.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search