Skip to content

Commit cddfad6

Browse files
authored
Update managed identity docs with latest best practices (#49696)
* Update managed identity docs with latest best practices * React to feedback
1 parent 280f8d7 commit cddfad6

File tree

5 files changed

+20
-160
lines changed

5 files changed

+20
-160
lines changed

docs/azure/sdk/authentication/system-assigned-managed-identity.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Authenticate Azure-hosted .NET apps to Azure resources using a system-ass
33
description: Learn how to authenticate Azure-hosted .NET apps to other Azure services using a system-assigned managed identity.
44
ms.topic: how-to
55
ms.custom: devx-track-dotnet, engagement-fy23, devx-track-azurecli
6-
ms.date: 02/06/2025
6+
ms.date: 11/10/2025
77
---
88

99
# Authenticate Azure-hosted .NET apps to Azure resources using a system-assigned managed identity

docs/azure/sdk/includes/implement-system-assigned-identity.md

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
ms.topic: include
3-
ms.date: 02/12/2025
3+
ms.date: 11/10/2025
44
---
55

66
[!INCLUDE [implement-managed-identity-concepts](implement-managed-identity-concepts.md)]
@@ -30,19 +30,10 @@ Azure services are accessed using specialized client classes from the various Az
3030

3131
1. Include the `Azure.Identity` and `Microsoft.Extensions.Azure` namespaces via `using` directives.
3232
1. Register the Azure service client using the corresponding `Add`-prefixed extension method.
33-
1. Pass an appropriate `TokenCredential` instance to the `UseCredential` method:
34-
- Use `DefaultAzureCredential` when your app is running locally.
35-
- Use `ManagedIdentityCredential` when your app is running in Azure.
33+
1. Use an appropriate `TokenCredential` instance for the environment in which your app is running. When your app is running:
34+
- In Azure, pass an instance of `ManagedIdentityCredential` to the `UseCredential` method. `ManagedIdentityCredential` discovers your managed identity configurations to authenticate to other services automatically.
35+
- On your local development machine, an instance of `DefaultAzureCredential` is created on your behalf. Call `UseCredential` only if you want to [customize `DefaultAzureCredential`](../authentication/credential-chains.md#how-to-customize-defaultazurecredential) or use a different credential. `DefaultAzureCredential` looks in the environment variables for an application service principal or at locally installed developer tools, such as Visual Studio, for a set of developer credentials.
3636

3737
:::code language="csharp" source="../snippets/authentication/system-assigned-managed-identity/Program.cs" id="snippet_MIC_UseCredential":::
3838

39-
An alternative to the `UseCredential` method is to provide the credential to the service client directly:
40-
41-
:::code language="csharp" source="../snippets/authentication/system-assigned-managed-identity/Program.cs" id="snippet_MIC":::
42-
4339
---
44-
45-
The preceding code behaves differently depending on the environment where it's running:
46-
47-
- On your local development workstation, `DefaultAzureCredential` looks in the environment variables for an application service principal or at locally installed developer tools, such as Visual Studio, for a set of developer credentials.
48-
- When deployed to Azure, `ManagedIdentityCredential` discovers your managed identity configurations to authenticate to other services automatically.

docs/azure/sdk/includes/implement-user-assigned-identity.md

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
ms.topic: include
3-
ms.date: 02/12/2025
3+
ms.date: 11/10/2025
44
---
55

66
[!INCLUDE [implement-managed-identity-concepts](implement-managed-identity-concepts.md)]
@@ -30,9 +30,9 @@ Azure services are accessed using specialized client classes from the various Az
3030

3131
1. Include the `Azure.Identity` and `Microsoft.Extensions.Azure` namespaces via `using` directives.
3232
1. Register the Azure service client using the corresponding `Add`-prefixed extension method.
33-
1. Pass an appropriate `TokenCredential` instance to the `UseCredential` method:
34-
- Use `DefaultAzureCredential` when your app is running locally
35-
- Use `ManagedIdentityCredential` when your app is running in Azure and configure either the client ID, resource ID, or object ID.
33+
1. Use an appropriate `TokenCredential` instance for the environment in which your app is running. When your app is running:
34+
- In Azure, pass an instance of `ManagedIdentityCredential` to the `UseCredential` method and configure either the client ID, resource ID, or object ID. `ManagedIdentityCredential` discovers your managed identity configurations to authenticate to other services automatically.
35+
- On your local development machine, an instance of `DefaultAzureCredential` is created on your behalf. Call `UseCredential` only if you want to [customize `DefaultAzureCredential`](../authentication/credential-chains.md#how-to-customize-defaultazurecredential) or use a different credential. `DefaultAzureCredential` looks in the environment variables for an application service principal or at locally installed developer tools, such as Visual Studio, for a set of developer credentials.
3636

3737
## [Client ID](#tab/client-id)
3838

@@ -51,10 +51,6 @@ The client ID is used to identify a managed identity when configuring applicatio
5151
5252
:::code language="csharp" source="../snippets/authentication/user-assigned-managed-identity/Program.cs" id="snippet_MIC_ClientId_UseCredential":::
5353
54-
An alternative to the `UseCredential` method is to provide the credential to the service client directly:
55-
56-
:::code language="csharp" source="../snippets/authentication/user-assigned-managed-identity/Program.cs" id="snippet_MIC_ClientId":::
57-
5854
## [Resource ID](#tab/resource-id)
5955
6056
The resource ID uniquely identifies the managed identity resource within your Azure subscription using the following structure:
@@ -76,10 +72,6 @@ Resource IDs can be built by convention, which makes them more convenient when w
7672
7773
:::code language="csharp" source="../snippets/authentication/user-assigned-managed-identity/Program.cs" id="snippet_MIC_ResourceId_UseCredential":::
7874
79-
An alternative to the `UseCredential` method is to provide the credential to the service client directly:
80-
81-
:::code language="csharp" source="../snippets/authentication/user-assigned-managed-identity/Program.cs" id="snippet_MIC_ResourceId":::
82-
8375
## [Object ID](#tab/object-id)
8476
8577
A principal ID is another name for an object ID.
@@ -97,13 +89,4 @@ A principal ID is another name for an object ID.
9789
9890
:::code language="csharp" source="../snippets/authentication/user-assigned-managed-identity/Program.cs" id="snippet_MIC_ObjectId_UseCredential":::
9991
100-
An alternative to the `UseCredential` method is to provide the credential to the service client directly:
101-
102-
:::code language="csharp" source="../snippets/authentication/user-assigned-managed-identity/Program.cs" id="snippet_MIC_ObjectId":::
103-
10492
---
105-
106-
The preceding code behaves differently depending on the environment where it's running:
107-
108-
- On your local development workstation, `DefaultAzureCredential` looks in the environment variables for an application service principal or at locally installed developer tools, such as Visual Studio, for a set of developer credentials.
109-
- When deployed to Azure, `ManagedIdentityCredential` discovers your managed identity configurations to authenticate to other services automatically.
Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using Azure.Identity;
22
using Microsoft.Extensions.Azure;
3-
using Azure.Storage.Blobs;
4-
using Azure.Core;
53

64
var builder = WebApplication.CreateBuilder(args);
75

@@ -11,42 +9,15 @@
119
clientBuilder.AddBlobServiceClient(
1210
new Uri("https://<account-name>.blob.core.windows.net"));
1311

14-
TokenCredential credential = null;
15-
16-
if (builder.Environment.IsProduction())
12+
if (builder.Environment.IsProduction() || builder.Environment.IsStaging())
1713
{
1814
// Managed identity token credential discovered when running in Azure environments
19-
credential = new ManagedIdentityCredential();
20-
}
21-
else
22-
{
23-
// Running locally on dev machine - DO NOT use in production or outside of local dev
24-
credential = new DefaultAzureCredential();
15+
ManagedIdentityCredential credential = new(ManagedIdentityId.SystemAssigned);
16+
clientBuilder.UseCredential(credential);
2517
}
26-
27-
clientBuilder.UseCredential(credential);
2818
});
2919
#endregion snippet_MIC_UseCredential
3020

31-
#region snippet_MIC
32-
TokenCredential credential = null;
33-
34-
if (builder.Environment.IsProduction() || builder.Environment.IsStaging())
35-
{
36-
// Managed identity token credential discovered when running in Azure environments
37-
credential = new ManagedIdentityCredential();
38-
}
39-
else
40-
{
41-
// Running locally on dev machine - DO NOT use in production or outside of local dev
42-
credential = new DefaultAzureCredential();
43-
}
44-
45-
builder.Services.AddSingleton<BlobServiceClient>(_ =>
46-
new BlobServiceClient(
47-
new Uri("https://<account-name>.blob.core.windows.net"), credential));
48-
#endregion snippet_MIC
49-
5021
var app = builder.Build();
5122

5223
if (app.Environment.IsDevelopment())
@@ -82,4 +53,4 @@
8253
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
8354
{
8455
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
85-
}
56+
}
Lines changed: 7 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Azure.Identity;
22
using Microsoft.Extensions.Azure;
3-
using Azure.Storage.Blobs;
43
using Azure.Core;
54

65
var builder = WebApplication.CreateBuilder(args);
@@ -50,43 +49,15 @@ void registerUsingClientId(WebApplicationBuilder builder)
5049
clientBuilder.AddBlobServiceClient(
5150
new Uri("https://<account-name>.blob.core.windows.net"));
5251

53-
TokenCredential credential = null;
54-
5552
if (builder.Environment.IsProduction() || builder.Environment.IsStaging())
5653
{
5754
// Managed identity token credential discovered when running in Azure environments
58-
credential = new ManagedIdentityCredential(
55+
ManagedIdentityCredential credential = new(
5956
ManagedIdentityId.FromUserAssignedClientId("<client-id>"));
57+
clientBuilder.UseCredential(credential);
6058
}
61-
else
62-
{
63-
// Running locally on dev machine - DO NOT use in production or outside of local dev
64-
credential = new DefaultAzureCredential();
65-
}
66-
67-
clientBuilder.UseCredential(credential);
6859
});
6960
#endregion snippet_MIC_ClientId_UseCredential
70-
71-
#region snippet_MIC_ClientId
72-
TokenCredential credential = null;
73-
74-
if (builder.Environment.IsProduction() || builder.Environment.IsStaging())
75-
{
76-
// Managed identity token credential discovered when running in Azure environments
77-
credential = new ManagedIdentityCredential(
78-
ManagedIdentityId.FromUserAssignedClientId("<client-id>"));
79-
}
80-
else
81-
{
82-
// Running locally on dev machine - DO NOT use in production or outside of local dev
83-
credential = new DefaultAzureCredential();
84-
}
85-
86-
builder.Services.AddSingleton<BlobServiceClient>(_ =>
87-
new BlobServiceClient(
88-
new Uri("https://<account-name>.blob.core.windows.net"), credential));
89-
#endregion snippet_MIC_ClientId
9061
}
9162

9263
void registerUsingObjectId(WebApplicationBuilder builder)
@@ -97,43 +68,15 @@ void registerUsingObjectId(WebApplicationBuilder builder)
9768
clientBuilder.AddBlobServiceClient(
9869
new Uri("https://<account-name>.blob.core.windows.net"));
9970

100-
TokenCredential credential = null;
101-
10271
if (builder.Environment.IsProduction() || builder.Environment.IsStaging())
10372
{
10473
// Managed identity token credential discovered when running in Azure environments
105-
credential = new ManagedIdentityCredential(
74+
ManagedIdentityCredential credential = new(
10675
ManagedIdentityId.FromUserAssignedObjectId("<object-id>"));
76+
clientBuilder.UseCredential(credential);
10777
}
108-
else
109-
{
110-
// Running locally on dev machine - DO NOT use in production or outside of local dev
111-
credential = new DefaultAzureCredential();
112-
}
113-
114-
clientBuilder.UseCredential(credential);
11578
});
11679
#endregion snippet_MIC_ObjectId_UseCredential
117-
118-
#region snippet_MIC_ObjectId
119-
TokenCredential credential = null;
120-
121-
if (builder.Environment.IsProduction() || builder.Environment.IsStaging())
122-
{
123-
// Managed identity token credential discovered when running in Azure environments
124-
credential = new ManagedIdentityCredential(
125-
ManagedIdentityId.FromUserAssignedObjectId("<object-id>"));
126-
}
127-
else
128-
{
129-
// Running locally on dev machine - DO NOT use in production or outside of local dev
130-
credential = new DefaultAzureCredential();
131-
}
132-
133-
builder.Services.AddSingleton<BlobServiceClient>(_ =>
134-
new BlobServiceClient(
135-
new Uri("https://<account-name>.blob.core.windows.net"), credential));
136-
#endregion snippet_MIC_ObjectId
13780
}
13881

13982

@@ -145,46 +88,18 @@ void registerUsingResourceId(WebApplicationBuilder builder)
14588
clientBuilder.AddBlobServiceClient(
14689
new Uri("https://<account-name>.blob.core.windows.net"));
14790

148-
TokenCredential credential = null;
149-
15091
if (builder.Environment.IsProduction() || builder.Environment.IsStaging())
15192
{
15293
// Managed identity token credential discovered when running in Azure environments
153-
credential = new ManagedIdentityCredential(
94+
ManagedIdentityCredential credential = new(
15495
ManagedIdentityId.FromUserAssignedResourceId(new ResourceIdentifier("<resource-id>")));
96+
clientBuilder.UseCredential(credential);
15597
}
156-
else
157-
{
158-
// Running locally on dev machine - DO NOT use in production or outside of local dev
159-
credential = new DefaultAzureCredential();
160-
}
161-
162-
clientBuilder.UseCredential(credential);
16398
});
16499
#endregion snippet_MIC_ResourceId_UseCredential
165-
166-
#region snippet_MIC_ResourceId
167-
TokenCredential credential = null;
168-
169-
if (builder.Environment.IsProduction() || builder.Environment.IsStaging())
170-
{
171-
// Managed identity token credential discovered when running in Azure environments
172-
credential = new ManagedIdentityCredential(
173-
ManagedIdentityId.FromUserAssignedResourceId(new ResourceIdentifier("<resource-id>")));
174-
}
175-
else
176-
{
177-
// Running locally on dev machine - DO NOT use in production or outside of local dev
178-
credential = new DefaultAzureCredential();
179-
}
180-
181-
builder.Services.AddSingleton<BlobServiceClient>(_ =>
182-
new BlobServiceClient(
183-
new Uri("https://<account-name>.blob.core.windows.net"), credential));
184-
#endregion snippet_MIC_ResourceId
185100
}
186101

187102
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
188103
{
189104
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
190-
}
105+
}

0 commit comments

Comments
 (0)