DefaultAzureCredential failed to retrieve a token from the included credentials.

Sean Ivins 0 Reputation points

I'm trying to use DefaultAzureCredential in a .NET 6 application to access an Azure Key Vault in the following code block:

public static IHostBuilder CreateHostBuilder(
    string[] args) =>
            .ConfigureAppConfiguration((context, config) =>
                // Build the injected config to get the KeyVaultName
                var builtConfig = config.Build();

                // Add secrets from Key Vault
                config.AddAzureKeyVault(new Uri($"https://{builtConfig["KeyVaultName"]}"), new DefaultAzureCredential());
            .ConfigureWebHostDefaults(webBuilder =>
Azure Key Vault
Azure Key Vault
An Azure service that is used to manage and protect cryptographic keys and other secrets used by cloud apps and services.
1,165 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Dronec 171 Reputation points

    VS is notorious for playing up with logged on credentials. I ended up creating a service principal that can access the resources I needed and specyfing its id and password in environmental variables on my dev machine. The variables are:




    DefaultAzureCredential works perfectly fine with them.