Skip to content

Commit c99a6c4

Browse files
authored
Add token lifetime and caching best practice (#44517)
* Add token lifetime and caching best practice * Fix link
1 parent e3ae3bd commit c99a6c4

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

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

Lines changed: 12 additions & 4 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: conceptual
5-
ms.date: 01/15/2025
5+
ms.date: 01/24/2025
66
---
77

88
# Authentication best practices with the Azure Identity library for .NET
@@ -25,7 +25,7 @@ For example, consider the following hypothetical sequence of events:
2525
To prevent these types of subtle issues or silent failures in production apps, strongly consider moving from `DefaultAzureCredential` to one of the following deterministic solutions:
2626

2727
- A specific `TokenCredential` implementation, such as `ManagedIdentityCredential`. See the [**Derived** list](/dotnet/api/azure.core.tokencredential?view=azure-dotnet&preserve-view=true#definition) for options.
28-
- A pared-down `ChainedTokenCredential` implementation optimized for the Azure environment in which your app runs. `ChainedTokenCredential` essentially creates a specific allow-list of acceptable credential options, such as `ManagedIdentity` for production and `VisualStudioCredential` for development.
28+
- A pared-down `ChainedTokenCredential` implementation optimized for the Azure environment in which your app runs. `ChainedTokenCredential` essentially creates a specific allowlist of acceptable credential options, such as `ManagedIdentity` for production and `VisualStudioCredential` for development.
2929

3030
For example, consider the following `DefaultAzureCredential` configuration in an ASP.NET Core project:
3131

@@ -60,13 +60,21 @@ For information on this approach, see [Authenticate using Microsoft Entra ID](/d
6060

6161
---
6262

63+
## Understand when token lifetime and caching logic is needed
64+
65+
If you use an Azure Identity library credential outside the context of [an Azure SDK client library that depends on Azure Core](../packages.md#libraries-using-azurecore), it becomes your responsibility to manage [token lifetime](/entra/identity-platform/access-tokens#token-lifetime) and caching behavior in your app.
66+
67+
The <xref:Azure.Core.AccessToken.RefreshOn%2A> property on `AccessToken`, which provides a hint to consumers as to when token refresh can be attempted, will be automatically used by Azure SDK client libraries that depend on the Azure Core library to refresh the token. For direct usage of Azure Identity library [credentials that support token caching](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/samples/TokenCache.md#credentials-supporting-token-caching), the underlying MSAL cache automatically refreshes proactively when the `RefreshOn` time occurs. This design allows the client code to call `GetToken` each time a token is needed and delegate the refresh to the library.
68+
69+
To only call `GetToken` when necessary, observe the `RefreshOn` date and proactively attempt to refresh the token after that time. The specific implementation is up to the customer.
70+
6371
## Understand the managed identity retry strategy
6472

6573
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:
6674

67-
- `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 is optimized to "fail fast" for an efficient development inner-loop.
75+
- `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.
6876
- Any other approach, such as `ChainedTokenCredential` or `ManagedIdentityCredential` directly:
69-
- 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.
77+
- 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.
7078
- 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:
7179

7280
:::code language="csharp" source="../snippets/authentication/best-practices/Program.cs" id="snippet_retries" highlight="5-9" :::

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
builder.Services.AddAzureClients(clientBuilder =>
1111
{
1212
clientBuilder.AddSecretClient(new Uri("<key-vault-url>"));
13-
clientBuilder.AddBlobServiceClient(new Uri("<blob-storage-uri>"));
13+
clientBuilder.AddBlobServiceClient(new Uri("<blob-storage-url>"));
1414

1515
clientBuilder.UseCredential(new DefaultAzureCredential());
1616
});
@@ -23,7 +23,7 @@
2323
new VisualStudioCredential());
2424

2525
BlobServiceClient blobServiceClient = new(
26-
new Uri("<blob-storage-uri>"),
26+
new Uri("<blob-storage-url>"),
2727
credentialChain);
2828

2929
SecretClient secretClient = new(

0 commit comments

Comments
 (0)