You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Create token credentials from configuration
3
-
description: This article describes how to create Microsoft Entra token credentials from configuration files.
2
+
title: Create Azure Identity library credentials via configuration files
3
+
description: Learn how to create token credentials from configuration files.
4
4
ms.topic: how-to
5
5
ms.custom: devx-track-dotnet, engagement-fy23
6
-
ms.date: 02/19/2025
6
+
ms.date: 03/14/2025
7
7
---
8
8
9
-
# Create Microsoft Entra credential types using configuration files
9
+
# Create Azure Identity library credentials via configuration files
10
10
11
-
The `Microsoft.Extensions.Azure` library supports creating different <xref:Azure.Core.TokenCredential?displayProperty=fullName> types from key-value pairs defined in _appsettings.json_ and other configuration files. The credential types correspond to a subset of the [credential classes](/dotnet/api/overview/azure/identity-readme) in the Azure Identity client library. This article describes the support for different `TokenCredential` types and how to configure the required key-value pairs for each type.
11
+
The [Azure client library integration for ASP.NET Core](/dotnet/api/overview/azure/microsoft.extensions.azure-readme?view=azure-dotnet&preserve-view=true) (`Microsoft.Extensions.Azure`) supports creating different <xref:Azure.Core.TokenCredential?displayProperty=fullName> types from key-value pairs defined in _appsettings.json_ and other configuration files. The credentials correspond to a subset of the [credential classes](/dotnet/api/overview/azure/identity-readme?view=azure-dotnet&preserve-view=true#credential-classes) in the Azure Identity client library. This article describes the support for different `TokenCredential` types and how to configure the required key-value pairs for each type.
12
12
13
13
## Support for Azure credentials through configuration
14
14
15
-
The [`Microsoft.Extensions.Azure`](https://www.nuget.org/packages/Microsoft.Extensions.Azure) library can automatically provide Azure service clients with a `TokenCredential` class by searching _appsettings.json_ or other configuration files for credential values using the `IConfiguration` abstraction for .NET. This approach allows developers to explicitly set credential values across different environments through configuration rather than through app code directly.
15
+
`Microsoft.Extensions.Azure` can automatically provide Azure service clients with a `TokenCredential` class by searching _appsettings.json_ or other configuration files for credential values using the `IConfiguration` abstraction for .NET. This approach allows developers to explicitly set credential values across different environments through configuration rather than through app code directly.
16
16
17
-
The following credential types are supported via configuration:
17
+
The following credentials can be created via configuration:
Azure service clients registered with the <xref:Microsoft.Extensions.Azure.AzureClientServiceCollectionExtensions.AddAzureClients%2A> method are automatically configured with an instance of `DefaultAzureCredential` if no explicit credential is supplied via the <xref:Microsoft.Extensions.Azure.AzureClientBuilderExtensions.WithCredential%2A> extension method. You can also override the global `DefaultAzureCredential` using credential values from configuration files when registering a client to create a specific credential type:
28
+
Azure service clients registered with the <xref:Microsoft.Extensions.Azure.AzureClientServiceCollectionExtensions.AddAzureClients%2A> method are automatically configured with an instance of `DefaultAzureCredential` if no explicit credential is supplied via the <xref:Microsoft.Extensions.Azure.AzureClientBuilderExtensions.WithCredential%2A> extension method. You can also override the global `DefaultAzureCredential` using credential values from configuration files when registering a client to create a specific credential:
28
29
29
30
```csharp
30
31
builder.Services.AddAzureClients(clientBuilder=>
31
32
{
32
-
// Register BlobServiceClient using credentials from appsettings.json
33
+
// Register BlobServiceClient using credential from appsettings.json
// Register ServiceBusClient using the fallback DefaultAzureCredential credentials
36
+
// Register ServiceBusClient using the fallback DefaultAzureCredential
36
37
clientBuilder.AddServiceBusClientWithNamespace(
37
38
"<your_namespace>.servicebus.windows.net");
38
39
});
@@ -44,27 +45,29 @@ The associated _appsettings.json_ file:
44
45
"Storage": {
45
46
"serviceUri": "<service_uri>",
46
47
"credential": "managedidentity",
47
-
"clientId": "<clientId>"
48
+
"clientId": "<client_id>"
48
49
}
49
50
```
50
51
51
-
The following credential types also support the `AdditionallyAllowedTenants` property, which specifies additional Microsoft Entra tenants beyond the default tenant for which the credential may acquire tokens:
52
+
The following credentials also support the `AdditionallyAllowedTenants` property, which specifies Microsoft Entra tenants beyond the default tenant for which the credential can acquire tokens:
Add the wildcard value "*" to allow the credential to acquire tokens for any Microsoft Entra tenant the logged in account can access. If no tenant IDs are specified, this option will have no effect on that authentication method, and the credential will acquire tokens for any requested tenant when using that method.
60
+
Add the wildcard value `*` to allow the credential to acquire tokens for any Microsoft Entra tenant the logged in account can access. If no tenant IDs are specified, this option has no effect on that authentication method, and the credential will acquire tokens for any requested tenant when using that method.
### Create an instance of `ManagedIdentityCredential`
66
69
67
-
You can create both user-assigned and system-assigned managed identities using configuration values. Add the following key-value pairs to your _appsettings.json_ file to create an instance of <xref:Azure.Identity.ManagedIdentityCredential?displayProperty=fullName>.
70
+
You can create both user-assigned and system-assigned managed identities using configuration values. To create an instance of <xref:Azure.Identity.ManagedIdentityCredential?displayProperty=fullName>, add the following key-value pairs to your _appsettings.json_ file.
68
71
69
72
#### User-assigned managed identities
70
73
@@ -75,7 +78,7 @@ A user-assigned managed identity can be used by providing a client ID, resource
75
78
```json
76
79
{
77
80
"credential": "managedidentity",
78
-
"clientId": "<clientId>"
81
+
"clientId": "<client_id>"
79
82
}
80
83
```
81
84
@@ -84,7 +87,7 @@ A user-assigned managed identity can be used by providing a client ID, resource
@@ -113,56 +116,75 @@ The resource ID takes the form:
113
116
}
114
117
```
115
118
116
-
### Create a `WorkloadIdentityCredential` type
119
+
### Create an instance of `AzurePipelinesCredential`
117
120
118
-
Add the following key-value pairs to your _appsettings.json_ file to create an <xref:Azure.Identity.WorkloadIdentityCredential?displayProperty=fullName>:
121
+
To create an instance of <xref:Azure.Identity.AzurePipelinesCredential?displayProperty=fullName>, add the following key-value pairs to your _appsettings.json_ file:
122
+
123
+
```json
124
+
{
125
+
"credential": "azurepipelines",
126
+
"clientId": "<client_id>",
127
+
"tenantId": "<tenant_id>",
128
+
"serviceConnectionId": "<service_connection_id>",
129
+
"systemAccessToken": "<system_access_token>"
130
+
}
131
+
```
132
+
133
+
> [!IMPORTANT]
134
+
> `AzurePipelinesCredential` is supported in `Microsoft.Extensions.Azure` versions 1.11.0 and later.
135
+
136
+
### Create an instance of `WorkloadIdentityCredential`
137
+
138
+
To create an instance of <xref:Azure.Identity.WorkloadIdentityCredential?displayProperty=fullName>, add the following key-value pairs to your _appsettings.json_ file:
119
139
120
140
```json
121
141
{
122
142
"credential": "workloadidentity",
123
-
"tenantId": "<tenantId>",
124
-
"clientId": "<clientId>",
125
-
"tokenFilePath": "<tokenFilePath>"
143
+
"tenantId": "<tenant_id>",
144
+
"clientId": "<client_id>",
145
+
"tokenFilePath": "<token_file_path>"
126
146
}
127
147
```
128
148
129
-
### Create a `ClientSecretCredential` type
149
+
### Create an instance of `ClientSecretCredential`
130
150
131
-
Add the following key-value pairs to your _appsettings.json_ file to create an <xref:Azure.Identity.ClientSecretCredential?displayProperty=fullName>:
151
+
To create an instance of <xref:Azure.Identity.ClientSecretCredential?displayProperty=fullName>, add the following key-value pairs to your _appsettings.json_ file:
132
152
133
153
```json
134
154
{
135
-
"tenantId": "<tenantId>",
136
-
"clientId": "<clientId>",
137
-
"clientSecret": "<clientSecret>"
155
+
"tenantId": "<tenant_id>",
156
+
"clientId": "<client_id>",
157
+
"clientSecret": "<client_secret>"
138
158
}
139
159
```
140
160
141
-
### Create a `ClientCertificateCredential` type
161
+
### Create an instance of `ClientCertificateCredential`
142
162
143
-
Add the following key-value pairs to your _appsettings.json_ file to create an <xref:Azure.Identity.ClientCertificateCredential?displayProperty=fullName>:
163
+
To create an instance of <xref:Azure.Identity.ClientCertificateCredential?displayProperty=fullName>, add the following key-value pairs to your _appsettings.json_ file:
> The `clientCertificateStoreLocation` and `additionallyAllowedTenants` key-value pairs are optional. If the keys are present and have empty values, they are ignored. If no `clientCertificateStoreLocation` is specified, the default `CurrentUser` is used from the <xref:System.Fabric.X509Credentials.StoreLocation?displayProperty=nameWithType> enum.
175
+
> The `clientCertificateStoreLocation` key is optional. If the key:
176
+
>
177
+
> * Is present and has an empty value, it's ignored.
178
+
> * Isn't present, the default `CurrentUser` is used from the <xref:System.Fabric.X509Credentials.StoreLocation?displayProperty=nameWithType> enum.
157
179
158
-
### Create a `DefaultAzureCredential` type
180
+
### Create an instance of `DefaultAzureCredential`
159
181
160
-
Add the following key-value pairs to your _appsettings.json_ file to create an <xref:Azure.Identity.DefaultAzureCredential?displayProperty=fullName>:
182
+
To create an instance of <xref:Azure.Identity.DefaultAzureCredential?displayProperty=fullName>, add the following key-value pairs to your _appsettings.json_ file:
0 commit comments