Authentification via Azure.Identity
In your code, you may have to provide the way you are authenticating to Microsoft Entra. Microsoft seems to push us to use the DefaultAzureCredentials but it ends up slowing down the startup of the application significantly. We recommend using the ChainedTokenCredentials instead, which will force the flow to use the AzureCli first, then managed entity, then the slow but maybe less restricted DefaultAzureCredentials.
AzureCli is being used by developers as well as Azure DevOps Service Connection.
ManagedIdentify is being used by AppServices.
Example of configuration when we want to add a KeyVault as configuration provider in a .Net app.
You first need to add the following Nuget Packages:
Azure.Extensions.AspNetCore.Configuration.Secrets
Azure.Identity
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.AddAppSettingsSecretsJson() //adding secrets json
.ConfigureAppConfiguration((_, builder) =>
{
var config = builder.Build();
var keyVaultUri = config["Settings:KeyVaultUri"]; //getting the key vault uri from the appsettings.json file
builder.AddAzureKeyVault(new Uri(keyVaultUri),
new ChainedTokenCredential(
new AzureCliCredential(),
new ManagedIdentityCredential(),
new DefaultAzureCredential()
),
new AzureKeyVaultConfigurationOptions()
{
ReloadInterval = TimeSpan.FromMinutes(30)
});
})
...
Reference:
https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication/?tabs=command-line