| title | Credential chains in the Azure Identity library for .NET |
|---|---|
| description | This article describes the DefaultAzureCredential and ChainedTokenCredential classes in the Azure Identity library. |
| ms.topic | concept-article |
| ms.date | 11/05/2025 |
The Azure Identity library provides credentials—public classes derived from the Azure Core library's TokenCredential class. A credential represents a distinct authentication flow for acquiring an access token from Microsoft Entra ID. These credentials can be chained together to form an ordered sequence of authentication mechanisms to be attempted.
At runtime, a credential chain attempts to authenticate using the sequence's first credential. If that credential fails to acquire an access token, the next credential in the sequence is attempted, and so on, until an access token is successfully obtained. The following sequence diagram illustrates this behavior:
:::image type="content" source="../media/mermaidjs/chain-sequence.svg" alt-text="Credential chain sequence diagram":::
A chained credential can offer the following benefits:
-
Environment awareness: Automatically selects the most appropriate credential based on the environment in which the app is running. Without it, you'd have to write code like this:
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_NoChain":::
-
Seamless transitions: Your app can move from local development to your staging or production environment without changing authentication code.
-
Improved resiliency: Includes a fallback mechanism that moves to the next credential when the prior fails to acquire an access token.
There are two disparate philosophies to credential chaining:
- "Tear down" a chain: Start with a preconfigured chain and exclude what you don't need. For this approach, see the DefaultAzureCredential overview section.
- "Build up" a chain: Start with an empty chain and include only what you need. For this approach, see the ChainedTokenCredential overview section.
DefaultAzureCredential is an opinionated, preconfigured chain of credentials. It's designed to support many environments, along with the most common authentication flows and developer tools. In graphical form, the underlying chain looks like this:
:::image type="content" source="../media/mermaidjs/default-azure-credential-authentication-flow-inline.svg" alt-text="Diagram that shows DefaultAzureCredential authentication flow." lightbox="../media/mermaidjs/default-azure-credential-authentication-flow-expanded.png":::
The order in which DefaultAzureCredential attempts credentials follows.
| Order | Credential | Description | Enabled by default? |
|---|---|---|---|
| 1 | Environment | Reads a collection of environment variables to determine if an application service principal (application user) is configured for the app. If so, DefaultAzureCredential uses these values to authenticate the app to Azure. This method is most often used in server environments but can also be used when developing locally. |
Yes |
| 2 | Workload Identity | If the app is deployed to an Azure host with Workload Identity enabled, authenticate that account. | Yes |
| 3 | Managed Identity | If the app is deployed to an Azure host with Managed Identity enabled, authenticate the app to Azure using that Managed Identity. | Yes |
| 4 | Visual Studio | If the developer authenticated to Azure by logging into Visual Studio, authenticate the app to Azure using that same account. | Yes |
| 5 | Visual Studio Code | If the developer authenticated via Visual Studio Code's Azure Resources extension and the Azure.Identity.Broker package is installed, authenticate that account. | Yes |
| 6 | Azure CLI | If the developer authenticated to Azure using Azure CLI's az login command, authenticate the app to Azure using that same account. |
Yes |
| 7 | Azure PowerShell | If the developer authenticated to Azure using Azure PowerShell's Connect-AzAccount cmdlet, authenticate the app to Azure using that same account. |
Yes |
| 8 | Azure Developer CLI | If the developer authenticated to Azure using Azure Developer CLI's azd auth login command, authenticate with that account. |
Yes |
| 9 | Interactive browser | If enabled, interactively authenticate the developer via the current system's default browser. | No |
| 10 | Broker | Authenticates using the default account logged into the OS via a broker. Requires that the Azure.Identity.Broker package is installed. | Yes |
In its simplest form, you can use the parameterless version of DefaultAzureCredential as follows:
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_Dac" highlight="8":::
Tip
The UseCredential method in the preceding code snippet is recommended for use in ASP.NET Core apps. For more information, see Use the Azure SDK for .NET in ASP.NET Core apps.
The following sections describe strategies for controlling which credentials are included in the chain.
To exclude an individual credential from DefaultAzureCredential, use the corresponding Exclude-prefixed property in DefaultAzureCredentialOptions. For example:
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_DacExcludes" highlight="11-13":::
In the preceding code sample, EnvironmentCredential, ManagedIdentityCredential, and WorkloadIdentityCredential are removed from the credential chain. As a result, the first credential to be attempted is VisualStudioCredential. The modified chain contains only development-time credentials and looks like this:
:::image type="content" source="../media/mermaidjs/default-azure-credential-excludes.svg" alt-text="DefaultAzureCredential using Excludes properties":::
Note
InteractiveBrowserCredential is excluded by default and therefore isn't shown in the preceding diagram. To include InteractiveBrowserCredential, either pass true to constructor xref:Azure.Identity.DefaultAzureCredential.%23ctor%28System.Boolean%29 or set property xref:Azure.Identity.DefaultAzureCredentialOptions.ExcludeInteractiveBrowserCredential%2A?displayProperty=nameWithType to false.
As more Exclude-prefixed properties are set to true (credential exclusions are configured), the advantages of using DefaultAzureCredential diminish. In such cases, ChainedTokenCredential is a better choice and requires less code. To illustrate, these two code samples behave the same way:
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_DacEquivalents":::
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_CtcEquivalents":::
To exclude all Developer tool or Deployed service credentials, set environment variable AZURE_TOKEN_CREDENTIALS to prod or dev, respectively. When a value of prod is used, the underlying credential chain looks as follows:
:::image type="content" source="../media/mermaidjs/default-azure-credential-environment-variable-production.svg" alt-text="DefaultAzureCredential with AZURE_TOKEN_CREDENTIALS set to 'prod'":::
When a value of dev is used, the chain looks as follows:
:::image type="content" source="../media/mermaidjs/default-azure-credential-environment-variable-development.svg" alt-text="DefaultAzureCredential with AZURE_TOKEN_CREDENTIALS set to 'dev'":::
To ensure the environment variable is defined and set to a supported string, use constructor overload xref:Azure.Identity.DefaultAzureCredential.%23ctor(System.String,Azure.Identity.DefaultAzureCredentialOptions)?displayProperty=name:
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_DacEnvVar" highlight="8-9":::
To exclude all credentials except for one, set environment variable AZURE_TOKEN_CREDENTIALS to the credential name. For example, you can reduce the DefaultAzureCredential chain to VisualStudioCredential by setting AZURE_TOKEN_CREDENTIALS to VisualStudioCredential. The string comparison is performed in a case-insensitive manner. Valid string values for the environment variable include:
AzureCliCredentialAzureDeveloperCliCredentialAzurePowerShellCredentialBrokerCredentialEnvironmentCredentialInteractiveBrowserCredentialManagedIdentityCredentialVisualStudioCredentialVisualStudioCodeCredentialWorkloadIdentityCredential
Important
The AZURE_TOKEN_CREDENTIALS environment variable supports individual credential names in Azure.Identity package versions 1.15.0 and later.
To ensure the environment variable is defined and set to a supported string, use constructor overload xref:Azure.Identity.DefaultAzureCredential.%23ctor(System.String,Azure.Identity.DefaultAzureCredentialOptions)?displayProperty=name:
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_DacEnvVar" highlight="8-9":::
ChainedTokenCredential is an empty chain to which you add credentials to suit your app's needs. For example:
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_Ctc" highlight="8-10" :::
The preceding code sample creates a tailored credential chain comprised of two development-time credentials. AzurePowerShellCredential is attempted first, followed by VisualStudioCredential, if necessary. In graphical form, the chain looks like this:
:::image type="content" source="../media/mermaidjs/chained-token-credential-authentication-flow.svg" alt-text="ChainedTokenCredential":::
Tip
For improved performance, optimize credential ordering in ChainedTokenCredential from most to least used credential.
DefaultAzureCredential is undoubtedly the easiest way to get started with the Azure Identity library, but with that convenience comes tradeoffs. Once you deploy your app to Azure, you should understand the app's authentication requirements. For that reason, replace DefaultAzureCredential with a specific TokenCredential implementation, such as ManagedIdentityCredential. See the Derived list for options.
Here's why:
- Debugging challenges: When authentication fails, it can be challenging to debug and identify the offending credential. You must enable logging to see the progression from one credential to the next and the success/failure status of each. For more information, see Debug a chained credential.
- Performance overhead: The process of sequentially trying multiple credentials can introduce performance overhead. For example, when running on a local development machine, managed identity is unavailable. Consequently,
ManagedIdentityCredentialalways fails in the local development environment, unless explicitly disabled via its correspondingExclude-prefixed property. - Unpredictable behavior:
DefaultAzureCredentialchecks for the presence of certain environment variables. It's possible that someone could add or modify these environment variables at the system level on the host machine. Those changes apply globally and therefore alter the behavior ofDefaultAzureCredentialat runtime in any app running on that machine. For more information on unpredictability, see Use deterministic credentials in production environments.
To diagnose an unexpected issue or to understand what a chained credential is doing, enable logging in your app. Optionally, filter the logs to only those events emitted from the Azure Identity library. For example:
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_FilteredLogging":::
For illustration purposes, assume the parameterless form of DefaultAzureCredential was used to authenticate a request to a Log Analytics workspace. The app ran in the local development environment, and Visual Studio was authenticated to an Azure account. The next time the app ran, the following pertinent entries appeared in the output:
:::code language="output" source="../snippets/authentication/credential-chains/dac-logs.txt":::
In the preceding output, notice that:
EnvironmentCredential,WorkloadIdentityCredential, andManagedIdentityCredentialeach failed to acquire a Microsoft Entra access token, in that order.- The
DefaultAzureCredential credential selected:-prefixed entry indicates the credential that was selected—VisualStudioCredentialin this case. SinceVisualStudioCredentialsucceeded, no credentials beyond it were used.