ASP.NET Core () Microsoft.Extensions.Azure
では、appsettings.jsonおよびその他の構成ファイルで定義されているキーと値のペアからさまざまなAzure.Core.TokenCredentialの種類を作成できます。 資格情報は、Azure ID クライアント ライブラリ内の 資格情報クラス のサブセットに対応します。 この記事では、さまざまな TokenCredential
の種類のサポートと、各種類に必要なキーと値のペアを構成する方法について説明します。
構成による Azure 資格情報のサポート
Microsoft.Extensions.Azure
では、.NET のTokenCredential
抽象化を使用して資格情報の値 appsettings.json またはその他の構成ファイルを検索することで、Azure サービス クライアントに IConfiguration
クラスを自動的に提供できます。 この方法を使用すると、開発者はアプリ コードを直接使用するのではなく、構成を使って異なる環境間で資格情報の値を明示的に設定できます。
構成を使用して、次の資格情報を作成できます。
- AzurePipelinesCredential
- ClientCertificateCredential
- ClientSecretCredential
- DefaultAzureCredential
- ManagedIdentityCredential
- WorkloadIdentityCredential
Azure 資格情報を構成する
AddAzureClients メソッドに登録された Azure サービス クライアントは、DefaultAzureCredential
拡張メソッドを使用して明示的な資格情報が指定されていない場合、WithCredential のインスタンスで自動的に構成されます。 また、クライアントを登録して特定の資格情報を作成するときに、構成ファイルの資格情報値を使用してグローバル DefaultAzureCredential
をオーバーライドすることもできます。
builder.Services.AddAzureClients(clientBuilder =>
{
// Register BlobServiceClient using credential from appsettings.json
clientBuilder.AddBlobServiceClient(builder.Configuration.GetSection("Storage"));
// Register ServiceBusClient using the fallback DefaultAzureCredential
clientBuilder.AddServiceBusClientWithNamespace(
"<your_namespace>.servicebus.windows.net");
});
関連付けられている appsettings.json ファイル:
"Storage": {
"serviceUri": "<service_uri>",
"credential": "managedidentity",
"clientId": "<client_id>"
}
次の資格情報では、 AdditionallyAllowedTenants
プロパティもサポートされています。このプロパティは、資格情報がトークンを取得できる既定のテナントを超える Microsoft Entra テナントを指定します。
- AzurePipelinesCredential
- ClientCertificateCredential
- ClientSecretCredential
- DefaultAzureCredential
- WorkloadIdentityCredential
ログイン アカウントがアクセス可能な任意の Microsoft Entra テナントのトークンを資格情報で取得できるようにするには、ワイルドカード値 *
を追加します。 テナント ID が指定されていない場合、このオプションはその認証方法に影響を与えず、資格情報はその方法を使用するときに要求されたテナントのトークンを取得します。
{
"additionallyAllowedTenants": "<tenant_ids_separated_by_semicolon>"
}
ManagedIdentityCredential
のインスタンスを作成する
構成値を使用して、次の方法でマネージド ID を利用するように資格情報を構成できます。
- システムによって割り当てられた管理ID
- ユーザーが割り当てた管理ID
- フェデレーション ID 資格情報としてのマネージド ID
Azure.Identity.ManagedIdentityCredentialのインスタンスを作成するには、appsettings.jsonファイルに次のキーと値のペアを追加します。
システムによって割り当てられた管理ID
{
"credential": "managedidentity"
}
ユーザーが割り当てた管理ID
ユーザー割り当てマネージド ID は、クライアント ID、リソース ID、またはオブジェクト ID を指定することで使用できます。
{
"credential": "managedidentity",
"managedIdentityClientId": "<managed_identity_client_id>"
}
フェデレーション ID 資格情報としてのマネージド ID
フェデレーション ID 資格情報機能としてのマネージド ID は、Microsoft.Extensions.Azure
バージョン 1.12.0 以降でサポートされています。 この機能は、システム割り当てマネージド ID では機能しません。 資格情報は、クライアント ID、リソース ID、またはオブジェクト ID を指定することで、ユーザー割り当てマネージド ID で構成できます。
{
"credential": "managedidentityasfederatedidentity",
"azureCloud": "<azure_cloud>",
"tenantId": "<tenant_id>",
"clientId": "<client_id>",
"managedIdentityClientId": "<managed_identity_client_id>"
}
azureCloud
キー値は、Microsoft Entra アクセス トークンスコープを設定するために使用されます。 次のいずれかの値を指定できます。
-
public
Azure パブリック クラウド用 -
usgov
Azure米国政府クラウド用に -
china
21Vianet が運用する Azure の場合
AzurePipelinesCredential
のインスタンスを作成する
Azure.Identity.AzurePipelinesCredentialのインスタンスを作成するには、appsettings.jsonファイルに次のキーと値のペアを追加します。
{
"credential": "azurepipelines",
"clientId": "<client_id>",
"tenantId": "<tenant_id>",
"serviceConnectionId": "<service_connection_id>",
"systemAccessToken": "<system_access_token>"
}
重要
AzurePipelinesCredential
は、 Microsoft.Extensions.Azure
バージョン 1.11.0 以降でサポートされています。
WorkloadIdentityCredential
のインスタンスを作成する
Azure.Identity.WorkloadIdentityCredentialのインスタンスを作成するには、appsettings.jsonファイルに次のキーと値のペアを追加します。
{
"credential": "workloadidentity",
"tenantId": "<tenant_id>",
"clientId": "<client_id>",
"tokenFilePath": "<token_file_path>"
}
ClientSecretCredential
のインスタンスを作成する
Azure.Identity.ClientSecretCredentialのインスタンスを作成するには、appsettings.jsonファイルに次のキーと値のペアを追加します。
{
"tenantId": "<tenant_id>",
"clientId": "<client_id>",
"clientSecret": "<client_secret>"
}
ClientCertificateCredential
のインスタンスを作成する
Azure.Identity.ClientCertificateCredentialのインスタンスを作成するには、appsettings.jsonファイルに次のキーと値のペアを追加します。
{
"tenantId": "<tenant_id>",
"clientId": "<client_id>",
"clientCertificate": "<client_certificate>",
"clientCertificateStoreLocation": "<client_certificate_store_location>"
}
注
clientCertificateStoreLocation
キーは省略可能です。 キーが次の場合:
- 存在している場合、値が空であれば、無視されます。
- 存在しません。既定の
CurrentUser
は、 X509Credentials.StoreLocation 列挙型から使用されます。
DefaultAzureCredential
のインスタンスを作成する
Azure.Identity.DefaultAzureCredentialのインスタンスを作成するには、appsettings.jsonファイルに次のキーと値のペアを追加します。
{
"tenantId": "<tenant_id>",
"clientId": "<client_id>",
"managedIdentityResourceId": "<managed_identity_resource_id>"
}
.NET