0

I am facing an issue while configuring Azure Key vault in Asp.net core Web API project .

Below is the code snippet as well as error for reference and I tried to find the root cause but no luck.

enter image description here

enter image description here

Error while Run() method execution.

enter image description here

Exception details enter image description here

Please help me out in solving this issue .Thanks in advance.

Ajit
  • 29
  • 6

1 Answers1

1

It can be issue from .net end mostly ,in .NET Core 3.0.100 or 3.1. This seems to occur when running under the debugger in Visual Studio mostly and when long parallel calls are made and retrypolicy may help stop this from giving exception for sometime and tries again.

  1. So Please try to Upgrade/Update the .NET Project SDK if any updates available in Visual Studio.
  2. Try running with command line

Also in the message of error you can see retry faild after 4 tries..

You can check ReloadInterval Property which is used in 3.0 and 3.1

builder.AddAzureKeyVault(
    new Uri(Configuration["KeyVault:URI"]), 
    new DefaultAzureCredential(
        new DefaultAzureCredentialOptions
        {
            ExcludeSharedTokenCacheCredential = true,
            VisualStudioTenantId = Configuration["AzureAd:TenantId"]
        }), 
    new AzureKeyVaultConfigurationOptions() 
    {
        ReloadInterval = TimeSpan.FromMinutes(15)
    }
);

You could try catching this exception and implementing a retry mechanism for your code if this exception is thrown so that it could try with retry time and delay required for next attempt. Azure Key Vault throttling guidance | Microsoft Docs

SecretClientOptions options = new SecretClientOptions()
    {
        Retry =
        {
            Delay= TimeSpan.FromSeconds(2),
            MaxDelay = TimeSpan.FromSeconds(16),
            MaxRetries = 5,
            Mode = RetryMode.Exponential
         }
    };
    var client = new SecretClient(new Uri("https://keyVaultName.vault.azure.net"), new DefaultAzureCredential(),options);
                                 
    //Retrieve Secret
    secret = client.GetSecret(secretName);

Also see GitHub discussion

  • If still issue remains, it may be calling several times due to network issue also.So please check the network , firewall and if there is any DNS issue for that endpoint.
  • Check URI if endpoint is incorrect or Managed Identity does not have Data owner or Reader role.Please make sure that you have proper permissions to access azure keyvault and give proper access policies atleast get , list and create if needed .
  • Make sure to have one of the roles(RBAC) provided here to access the keyvault .

References:

  1. asp.net core - How to configure Azure KeyVault refresh interval with the Azure.Security.KeyVault libraries - Stack Overflow
  2. azure sdk .net issues(github)
kavyaS
  • 8,026
  • 1
  • 7
  • 19