Skip to content

Commit ed032dd

Browse files
committed
Update managed identity retry strategy section
1 parent c2fc4a3 commit ed032dd

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

docs/azure/sdk/authentication/best-practices.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Authentication best practices with the Azure Identity library for .NET
33
description: This article describes authentication best practices to follow when using the Azure Identity library for .NET.
44
ms.topic: concept-article
5-
ms.date: 02/14/2025
5+
ms.date: 09/17/2025
66
---
77

88
# Authentication best practices with the Azure Identity library for .NET
@@ -71,13 +71,35 @@ To only call `GetToken` when necessary, observe the `RefreshOn` date and proacti
7171

7272
## Understand the managed identity retry strategy
7373

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.
7575

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+
1. "Fail fast" mode
8077

81-
:::code language="csharp" source="../snippets/authentication/best-practices/CCA/Program.cs" id="snippet_retries" highlight="5-9":::
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.
8283

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).
84+
1. 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 `AZURE_TOKEN_CREDENTIALS` environment variable set to `ManagedIdentityCredential`
89+
- Use `ChainedTokenCredential` containing `ManagedIdentityCredential`
90+
- Use `ManagedIdentityCredential` directly
91+
- **How it works:** The time interval between retries starts at 0.8 seconds, and a maximum of five retries are attempted, by default. This mode is optimized for resilience but introduces potentially unwanted delays in the development inner loop.
92+
93+
To change the default retry settings, use the <xref:Azure.Core.ClientOptions.Retry%2A> property. For example, retry a maximum of three times, with a starting interval of 0.5 seconds:
94+
95+
# [DefaultAzureCredential](#tab/dac)
96+
97+
:::code language="csharp" source="../snippets/authentication/best-practices/CCA/Program.cs" id="snippet_retries_dac" highlight="5-9":::
98+
99+
# [ManagedIdentityCredential](#tab/mic)
100+
101+
:::code language="csharp" source="../snippets/authentication/best-practices/CCA/Program.cs" id="snippet_retries_mic" highlight="5-9":::
102+
103+
---
104+
105+
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).

docs/azure/sdk/snippets/authentication/best-practices/CCA/Program.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
});
3737
#endregion snippet_credential_reuse_AspNetCore
3838

39-
#region snippet_retries
39+
#region snippet_retries_mic
4040
ManagedIdentityCredentialOptions miCredentialOptions = new(
4141
ManagedIdentityId.FromUserAssignedClientId(clientId)
4242
)
@@ -51,6 +51,20 @@
5151
ManagedIdentityCredential miCredential = new(miCredentialOptions);
5252
#endregion
5353

54+
#region snippet_retries_dac
55+
DefaultAzureCredential credential = new(
56+
new DefaultAzureCredentialOptions
57+
{
58+
ManagedIdentityClientId = clientId,
59+
Retry =
60+
{
61+
MaxRetries = 3,
62+
Delay = TimeSpan.FromSeconds(0.5),
63+
}
64+
}
65+
);
66+
#endregion
67+
5468
builder.Services.AddEndpointsApiExplorer();
5569

5670
var app = builder.Build();

0 commit comments

Comments
 (0)