|
2 | 2 | title: Authentication best practices with the Azure Identity library for .NET
|
3 | 3 | description: This article describes authentication best practices to follow when using the Azure Identity library for .NET.
|
4 | 4 | ms.topic: concept-article
|
5 |
| -ms.date: 02/14/2025 |
| 5 | +ms.date: 09/17/2025 |
6 | 6 | ---
|
7 | 7 |
|
8 | 8 | # Authentication best practices with the Azure Identity library for .NET
|
@@ -71,13 +71,31 @@ To only call `GetToken` when necessary, observe the `RefreshOn` date and proacti
|
71 | 71 |
|
72 | 72 | ## Understand the managed identity retry strategy
|
73 | 73 |
|
74 |
| -The Azure Identity library for .NET allows you to authenticate via managed identity with `ManagedIdentityCredential`. The way in which you use `ManagedIdentityCredential` impacts the applied retry strategy. When used via: |
| 74 | +The Azure Identity library for .NET allows you to authenticate via managed identity with `ManagedIdentityCredential`. The mode in which you use `ManagedIdentityCredential` impacts the applied retry strategy. |
75 | 75 |
|
76 |
| -- `DefaultAzureCredential`, no retries are attempted when the initial token acquisition attempt fails or times out after a short duration. This is the least resilient option because it's optimized to "fail fast" for an efficient development inner loop. |
77 |
| -- Any other approach, such as `ChainedTokenCredential` or `ManagedIdentityCredential` directly: |
78 |
| - - The time interval between retries starts at 0.8 seconds, and a maximum of five retries are attempted, by default. This option is optimized for resilience but introduces potentially unwanted delays in the development inner loop. |
79 |
| - - To change any of the default retry settings, use the `Retry` property on `ManagedIdentityCredentialOptions`. For example, retry a maximum of three times, with a starting interval of 0.5 seconds: |
| 76 | +### "Fail fast" mode |
| 77 | + |
| 78 | +- **When to use:** For local development scenarios where you want quick feedback |
| 79 | +- **How to activate:** Use `DefaultAzureCredential` in one of the following ways: |
| 80 | + - Without setting environment variable `AZURE_TOKEN_CREDENTIALS` |
| 81 | + - With environment variable `AZURE_TOKEN_CREDENTIALS` set to a string other than `ManagedIdentityCredential` |
| 82 | +- **How it works:** No retries are attempted when the initial token acquisition attempt fails or times out after a short duration. This is the least resilient mode because it's optimized to "fail fast" for an efficient development inner loop. |
| 83 | + |
| 84 | +### Resilient mode |
| 85 | + |
| 86 | +- **When to use:** For production scenarios where resilience is important |
| 87 | +- **How to activate:** Take one of the following approaches: |
| 88 | + - Use `DefaultAzureCredential` with environment variable `AZURE_TOKEN_CREDENTIALS` set to `ManagedIdentityCredential` |
| 89 | + |
| 90 | + > [!IMPORTANT] |
| 91 | + > This `DefaultAzureCredential` approach only operates in resilient mode when using `Azure.Identity` package version 1.16.0 or later. In earlier versions, this approach operates in "fail fast" mode. |
| 92 | +
|
| 93 | + - Use `ChainedTokenCredential` containing `ManagedIdentityCredential` |
| 94 | + - Use `ManagedIdentityCredential` directly |
| 95 | +- **How it works:** The time interval between retries starts at 0.8 seconds, and a maximum of five retries are attempted with exponential backoff, by default. This mode is optimized for resilience but introduces potentially unwanted delays in the development inner loop. |
| 96 | + |
| 97 | + To change the default retry settings, use the <xref:Azure.Core.ClientOptions.Retry%2A> property on `DefaultAzureCredentialOptions` or `ManagedIdentityCredentialOptions`. For example, retry a maximum of three times, with a starting interval of 0.5 seconds: |
80 | 98 |
|
81 | 99 | :::code language="csharp" source="../snippets/authentication/best-practices/CCA/Program.cs" id="snippet_retries" highlight="5-9":::
|
82 | 100 |
|
83 |
| -For more information on customizing retry policies, see [Setting a custom retry policy](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Configuration.md#setting-a-custom-retry-policy). |
| 101 | + For more information on customizing retry policies, see [Setting a custom retry policy](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Configuration.md#setting-a-custom-retry-policy). |
0 commit comments