You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/javascript/sdk/authentication/credential-chains.md
+42-16Lines changed: 42 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Credential chains in the Azure library for JavaScript"
3
3
description: This article describes the DefaultAzureCredential and ChainedTokenCredential classes in the Azure Identity client library for JavaScript.
4
-
ms.date: 06/02/2025
4
+
ms.date: 08/08/2025
5
5
ms.topic: concept-article
6
6
ms.custom: devx-track-js
7
7
#customer intent: As a JavaScript developer new to Azure, I want understand credential chains so that select the appropriate chain and understand how to configure and debug it.
@@ -11,26 +11,26 @@ ms.custom: devx-track-js
11
11
12
12
The Azure Identity client library provides *credentials* which are public classes that implement the Azure Core library's [TokenCredential](/javascript/api/@azure/identity/tokencredential) interface. A credential represents a distinct authentication flow for acquiring an access token from Microsoft Entra ID. These credentials can be selected individually or chained together to form an ordered sequence of authentication mechanisms to be attempted.
13
13
14
-
***Individual credentials** provide speed and certainty. If they fail, you know the credential wasn't authenticated.
15
-
***Chains** provide fallbacks. When the credential fails to authenticate, the next credential in the chain is attempted.
14
+
-**Individual credentials** provide speed and certainty. If they fail, you know the credential wasn't authenticated.
15
+
-**Chains** provide fallbacks. When the credential fails to authenticate, the next credential in the chain is attempted.
16
16
17
17
## Design your authentication flows
18
18
19
19
When you use Azure SDK client libraries, the first step is to authenticate to Azure. There are many options of how to authenticate to consider, such as tools and IDEs used in the development team, automation workflows such as testing and CI/CD, and hosting platforms such as Azure App Service.
20
20
21
21
Choose from the following common progressions for your authentication flow:
22
22
23
-
* Use the `DefaultAzureCredential` for **teams whose developers use various IDEs and CLIs to authenticate to Azure**. This allows the greatest flexibility. This flexibility is provided at the cost of performance to validate the credentials in the chain until one succeeds.
23
+
- Use the `DefaultAzureCredential` for **teams whose developers use various IDEs and CLIs to authenticate to Azure**. This allows the greatest flexibility. This flexibility is provided at the cost of performance to validate the credentials in the chain until one succeeds.
24
24
25
25
- The fallback from credential to credential is selected on your behalf based on the detected environment.
26
26
- To determine which credential was selected, turn on [debugging](#debug-a-chained-credential).
27
27
28
-
* Use the `ChainedTokenCredential` for **teams which have a strict and scoped selection of tools**. For example, they all authenticate in and use the same IDE or CLI. This allows the team to select the exact credentials and the order which still provides flexibility but at a reduced performance cost.
28
+
- Use the `ChainedTokenCredential` for **teams which have a strict and scoped selection of tools**. For example, they all authenticate in and use the same IDE or CLI. This allows the team to select the exact credentials and the order which still provides flexibility but at a reduced performance cost.
29
29
30
30
- You select the fallback path from credential to credential regardless of the environment it's run in.
31
31
- To determine which credential was selected, turn on [debugging](#debug-a-chained-credential).
32
32
33
-
* For **teams with certainty of credentials** in all the environments, a control flow statement such as if/else, allows you to know which credential was chosen in each environment.
33
+
- For **teams with certainty of credentials** in all the environments, a control flow statement such as if/else, allows you to know which credential was chosen in each environment.
34
34
35
35
- There's no fallback to another credential type.
36
36
- You don't need to debug to determine which credential was chosen because it was specified.
@@ -45,25 +45,32 @@ At runtime, a credential chain attempts to authenticate using the sequence's fir
45
45
46
46
[DefaultAzureCredential](/javascript/api/%40azure/identity/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:
47
47
48
-
:::image type="content" source="../media/mermaidjs/default-azure-credential-auth-flow-inline.svg" alt-text="Diagram that shows DefaultAzureCredential authentication flow." lightbox="../media/mermaidjs/default-azure-credential-auth-flow-expanded.png":::
48
+
:::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":::
49
49
50
50
The order in which `DefaultAzureCredential` attempts credentials follows.
51
51
52
52
| Order | Credential | Description |
53
53
|-------|---------------------|-------------|
54
-
| 1 |[Environment][env-cred]|Reads a collection of [environment variables][env-vars] 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. |
55
-
| 2 |[Workload Identity][wi-cred]|If the app is deployed to an Azure host with Workload Identity enabled, authenticate that account. |
56
-
| 3 |[Managed Identity][mi-cred]|If the app is deployed to an Azure host with Managed Identity enabled, authenticate the app to Azure using that Managed Identity. |
57
-
| 4 |[Azure CLI][az-cred]|If the developer authenticated to Azure using Azure CLI's `az login` command, authenticate the app to Azure using that same account. |
58
-
| 5 |[Azure PowerShell][pwsh-cred]|If the developer authenticated to Azure using Azure PowerShell's `Connect-AzAccount` cmdlet, authenticate the app to Azure using that same account. |
59
-
| 6 |[Azure Developer CLI][azd-cred]|If the developer authenticated to Azure using Azure Developer CLI's `azd auth login` command, authenticate with that account. |
54
+
| 1 |[Environment][env-cred]|Reads a collection of [environment variables][env-vars] 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. |
55
+
| 2 |[Workload Identity][wi-cred]|If the app is deployed to an Azure host with Workload Identity enabled, authenticate that account. |
56
+
| 3 |[Managed Identity][mi-cred]|If the app is deployed to an Azure host with Managed Identity enabled, authenticate the app to Azure using that Managed Identity. |
57
+
| 4 |[Visual Studio Code][vsc-cred]|If the developer authenticated via Visual Studio Code's [Azure Resources extension][vsc-ext] and the [@azure/identity-vscode package][vsc-plugin-pkg] is installed, authenticate that account. |
58
+
| 5 |[Azure CLI][az-cred]|If the developer authenticated to Azure using Azure CLI's `az login` command, authenticate the app to Azure using that same account. |
59
+
| 6 |[Azure PowerShell][pwsh-cred]|If the developer authenticated to Azure using Azure PowerShell's `Connect-AzAccount` cmdlet, authenticate the app to Azure using that same account. |
60
+
| 7 |[Azure Developer CLI][azd-cred]|If the developer authenticated to Azure using Azure Developer CLI's `azd auth login` command, authenticate with that account. |
61
+
| 8 |[Broker][broker-cred]|Authenticates using the default account logged into the OS via a broker. Requires that the [@azure/identity-broker package][broker-plugin-pkg] is installed, since a broker-enabled instance of `InteractiveBrowserCredential` is used. |
In its simplest form, you can use the parameterless version of `DefaultAzureCredential` as follows:
69
76
@@ -86,6 +93,10 @@ const blobServiceClient = new BlobServiceClient(
86
93
87
94
### How to customize DefaultAzureCredential
88
95
96
+
The following sections describe strategies for controlling which credentials are included in the chain.
97
+
98
+
#### Exclude a credential type category
99
+
89
100
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:
90
101
91
102
:::image type="content" source="../media/mermaidjs/default-azure-credential-environment-variable-production.svg" alt-text="Diagram that shows DefaultAzureCredential with AZURE_TOKEN_CREDENTIALS set to 'prod'.":::
@@ -97,9 +108,24 @@ When a value of `dev` is used, the chain looks as follows:
97
108
> [!IMPORTANT]
98
109
> The `AZURE_TOKEN_CREDENTIALS` environment variable is supported in `@azure/identity` package versions 4.10.0 and later.
99
110
111
+
#### Use a specific credential
112
+
113
+
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 `AzureCliCredential` by setting `AZURE_TOKEN_CREDENTIALS` to `AzureCliCredential`. The string comparison is performed in a case-insensitive manner. Valid string values for the environment variable include:
114
+
115
+
-`AzureCliCredential`
116
+
-`AzureDeveloperCliCredential`
117
+
-`AzurePowerShellCredential`
118
+
-`EnvironmentCredential`
119
+
-`ManagedIdentityCredential`
120
+
-`VisualStudioCodeCredential`
121
+
-`WorkloadIdentityCredential`
122
+
123
+
> [!IMPORTANT]
124
+
> The `AZURE_TOKEN_CREDENTIALS` environment variable supports individual credential names in `@azure/identity` package versions 4.11.0 and later.
125
+
100
126
## Use ChainedTokenCredential for granularity
101
127
102
-
[ChainedTokenCredential](/javascript/api/@azure/identity/chainedtokencredential) is an empty chain to which you add credentials to suit your app's needs. For example, the following example adds a `ManagedIdentityCredential` instance, then an `AzureCliCredential` instance.
128
+
[ChainedTokenCredential](/javascript/api/@azure/identity/chainedtokencredential) is an empty chain to which you add credentials to suit your app's needs. For example, the following example adds a `ManagedIdentityCredential` instance, then an `AzureCliCredential` instance.
103
129
104
130
```javascript
105
131
import {
@@ -116,7 +142,7 @@ const credential = new ChainedTokenCredential(
116
142
117
143
The preceding code sample creates a tailored credential chain comprised of two credentials. The user-assigned managed identity variant of `ManagedIdentityCredential` is attempted first, followed by `AzureCliCredential`, if necessary. In graphical form, the chain looks like this:
118
144
119
-
:::image type="content" source="../media/mermaidjs/chained-token-credential-auth-flow.svg" alt-text="Diagram showing Azure Identity credential chain for managed identity and Azure CLI.":::
145
+
:::image type="content" source="../media/mermaidjs/chained-token-credential-authentication-flow.svg" alt-text="Diagram showing Azure Identity credential chain for managed identity and Azure CLI.":::
120
146
121
147
> [!TIP]
122
148
> For improved performance, optimize credential ordering for your **production environment**. Credentials intended for use in the local development environment should be added last.
@@ -127,7 +153,7 @@ To debug a credential chain, enable [Azure SDK logging](../debug-client-librarie
0 commit comments