Skip to content

Commit e793387

Browse files
authored
Add credential builder context (#43708)
* Add credential builder context
1 parent 88043a9 commit e793387

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

docs/azure/sdk/authentication/credential-chains.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ The order in which `DefaultAzureCredential` attempts credentials follows.
6363

6464
In its simplest form, you can use the parameterless version of `DefaultAzureCredential` as follows:
6565

66-
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_Dac" highlight="1":::
66+
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_Dac" highlight="6":::
6767

6868
> [!TIP]
6969
> The `UseCredential` method in the preceding code snippet is recommended for use in ASP.NET Core apps. For more information, see [Use the Azure SDK for .NET in ASP.NET Core apps](../aspnetcore-guidance.md#authenticate-using-microsoft-entra-id).
@@ -72,7 +72,7 @@ In its simplest form, you can use the parameterless version of `DefaultAzureCred
7272

7373
To remove a credential from `DefaultAzureCredential`, use the corresponding `Exclude`-prefixed property in [DefaultAzureCredentialOptions](/dotnet/api/azure.identity.defaultazurecredentialoptions?view=azure-dotnet&preserve-view=true#properties). For example:
7474

75-
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_DacExcludes" highlight="4-5":::
75+
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_DacExcludes" highlight="9-10":::
7676

7777
In the preceding code sample, `EnvironmentCredential` and `WorkloadIdentityCredential` are removed from the credential chain. As a result, the first credential to be attempted is `ManagedIdentityCredential`. The modified chain looks like this:
7878

@@ -97,7 +97,7 @@ As more `Exclude`-prefixed properties are set to `true` (credential exclusions a
9797

9898
[ChainedTokenCredential](/dotnet/api/azure.identity.chainedtokencredential?view=azure-dotnet&preserve-view=true) is an empty chain to which you add credentials to suit your app's needs. For example:
9999

100-
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_Ctc":::
100+
:::code language="csharp" source="../snippets/authentication/credential-chains/Program.cs" id="snippet_Ctc" highlight="6-8" :::
101101

102102
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 `VisualStudioCredential`, if necessary. In graphical form, the chain looks like this:
103103

docs/azure/sdk/snippets/authentication/credential-chains/Program.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,45 @@
1717
}, EventLevel.LogAlways);
1818
#endregion snippet_FilteredLogging
1919

20+
#region snippet_Dac
2021
builder.Services.AddAzureClients(clientBuilder =>
2122
{
2223
clientBuilder.AddBlobServiceClient(
2324
new Uri("https://<account-name>.blob.core.windows.net"));
24-
#region snippet_Dac
25+
2526
DefaultAzureCredential credential = new();
2627
clientBuilder.UseCredential(credential);
27-
#endregion snippet_Dac
28+
});
29+
#endregion snippet_Dac
30+
31+
#region snippet_DacExcludes
32+
builder.Services.AddAzureClients(clientBuilder =>
33+
{
34+
clientBuilder.AddBlobServiceClient(
35+
new Uri("https://<account-name>.blob.core.windows.net"));
2836

29-
#region snippet_DacExcludes
3037
clientBuilder.UseCredential(new DefaultAzureCredential(
3138
new DefaultAzureCredentialOptions
3239
{
3340
ExcludeEnvironmentCredential = true,
3441
ExcludeWorkloadIdentityCredential = true,
3542
ManagedIdentityClientId = userAssignedClientId,
3643
}));
37-
#endregion snippet_DacExcludes
44+
});
45+
#endregion snippet_DacExcludes
46+
47+
#region snippet_Ctc
48+
builder.Services.AddAzureClients(clientBuilder =>
49+
{
50+
clientBuilder.AddBlobServiceClient(
51+
new Uri("https://<account-name>.blob.core.windows.net"));
3852

39-
#region snippet_Ctc
4053
clientBuilder.UseCredential(new ChainedTokenCredential(
4154
new ManagedIdentityCredential(clientId: userAssignedClientId),
4255
new VisualStudioCredential()));
43-
#endregion snippet_Ctc
4456
});
57+
#endregion snippet_Ctc
58+
4559

4660
builder.Services.AddEndpointsApiExplorer();
4761
builder.Services.AddSwaggerGen();

0 commit comments

Comments
 (0)