共用方式為


透過組態檔建立 Azure 身分識別連結庫認證

ASP.NET Core 的 Azure 用戶端程式庫整合Microsoft.Extensions.Azure)支援從在 Azure.Core.TokenCredential 及其他組態檔案中定義的鍵值對建立不同類型。 認證會對應至 Azure 身分識別用戶端程式庫中認證類別的子集。 本文說明對不同 TokenCredential 類型的支援,以及如何為每個類型設定必要的機碼/值組。

透過組態支援 Azure 認證

Microsoft.Extensions.Azure 可以透過使用 .NET 的 TokenCredential 抽象概念,搜尋 appsettings.json 或其他組態檔中的憑證值,自動為 Azure 服務客戶端提供 IConfiguration 類別。 這種方法可讓開發人員透過組態,而不是直接透過應用程式程式碼,跨不同環境明確設定認證值。

您可以透過組態建立下列認證:

設定 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 租戶,不僅限於預設租戶:

新增通配符值 *,以允許認證取得登入帳戶可存取的任何 Microsoft Entra 租戶的令牌。 如果未指定任何租使用者標識符,此選項對該驗證方法沒有任何影響,而且認證會在使用該方法時取得任何要求的租使用者的令牌。

{
    "additionallyAllowedTenants": "<tenant_ids_separated_by_semicolon>"
}

建立 ManagedIdentityCredential 的實例

您可以使用組態值,以下列方式設定認證來利用受控識別:

  • 系統指派的管理身份識別
  • 使用者指派的受管理的身分識別
  • 受管理的身分識別作為聯邦身分認證

若要建立 Azure.Identity.ManagedIdentityCredential 的實例,請將下列的鍵值對新增至 您的appsettings.json 檔案。

系統指派的管理身份識別

{
    "credential": "managedidentity"
}

使用者指派的受管理的身分識別

提供用戶端識別碼、資源識別碼或對象識別碼,即可使用使用者指派的受控識別。

{
    "credential": "managedidentity",
    "managedIdentityClientId": "<managed_identity_client_id>"
}

受管理的身分識別作為聯邦身分認證

1.12.0 版和更新版本支援受控Microsoft.Extensions.Azure功能。 此功能不適用於系統指派的受控識別。 您可以藉由提供用戶端標識碼、資源識別碼或物件識別碼,以使用者指派的受控識別來設定認證。

{
    "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 1.11.0 版和更新版本支援 Microsoft.Extensions.Azure

建立 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>"
}