使用配置文件创建 Microsoft Entra 凭据类型

Microsoft.Extensions.Azure 库支持从 appsettings.json 和其他配置文件中定义的键值对创建不同的 Azure.Core.TokenCredential 类型。 凭据类型对应于 Azure 标识客户端库中凭据类的子集。 本文介绍了对不同 TokenCredential 类型的支持,以及如何为每个类型配置所需的键值对。

通过配置支持 Azure 凭据

Microsoft.Extensions.Azure 库可以通过使用适用于 .NET 的 IConfiguration 抽象在 appsettings.json 或其他配置文件中搜索凭据值,自动为 Azure 服务客户端提供 TokenCredential 类。 此方法允许开发人员通过配置(而不是直接通过应用代码)在不同的环境中显式设置凭据值。

通过配置支持以下凭据类型:

配置 Azure 凭据

使用 AddAzureClients 方法注册的 Azure 服务客户端会自动配置有 DefaultAzureCredential 的实例(如果未通过 WithCredential 扩展方法提供显式凭据)。 注册客户端以创建特定凭据类型时,还可以使用配置文件中的凭据值替代全局 DefaultAzureCredential

builder.Services.AddAzureClients(clientBuilder =>
{
    // Register BlobServiceClient using credentials from appsettings.json
    clientBuilder.AddBlobServiceClient(builder.Configuration.GetSection("Storage"));

    // Register ServiceBusClient using the fallback DefaultAzureCredential credentials
    clientBuilder.AddServiceBusClientWithNamespace(
        "<your_namespace>.servicebus.windows.net");
});

关联的 appsettings.json 文件:

"Storage": {
    "serviceUri": "<service_uri>",
    "credential": "managedidentity",
    "clientId":  "<clientId>"
}

以下凭据类型还支持 AdditionallyAllowedTenants 属性,该属性指定凭据可以为其获取令牌的默认租户之外的其他 Microsoft Entra 租户:

添加通配符值“*”可允许凭据获取登录帐户可以访问的任何 Microsoft Entra 租户的令牌。 如果未指定租户 ID,则此选项对该身份验证方法没有影响,并且在使用该方法时,凭据将为任何已请求的租户获取令牌。

{
    "additionallyAllowedTenants":  "<tenant-ids-separated-by-semicolon>"
}

创建 ManagedIdentityCredential 类型

可以使用配置值创建用户分配的托管标识和系统分配的托管标识。 将以下键值对添加到 appsettings.json 文件以创建 Azure.Identity.ManagedIdentityCredential 的实例。

用户分配的托管标识

通过客户端 ID 指定用户分配的托管标识:

{
    "credential": "managedidentity",
    "clientId":  "<clientId>"
}

或者,通过资源 ID 指定用户分配的托管标识:

{
    "credential": "managedidentity",
    "managedIdentityResourceId":  "<managedIdentityResourceId>"
}

资源 ID 采用 /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName} 形式。

系统分配的托管标识

{
    "credential": "managedidentity"
}

创建 WorkloadIdentityCredential 类型

将以下键值对添加到 appsettings.json 文件以创建 Azure.Identity.WorkloadIdentityCredential

{
    "credential": "workloadidentity",
    "tenantId":  "<tenantId>",
    "clientId":  "<clientId>",
    "tokenFilePath": "<tokenFilePath>"
}

创建 ClientSecretCredential 类型

将以下键值对添加到 appsettings.json 文件以创建 Azure.Identity.ClientSecretCredential

{
    "tenantId":  "<tenantId>",
    "clientId":  "<clientId>",
    "clientSecret": "<clientSecret>"
}

创建 ClientCertificateCredential 类型

将以下键值对添加到 appsettings.json 文件以创建 Azure.Identity.ClientCertificateCredential

{
    "tenantId":  "<tenantId>",
    "clientId":  "<clientId>",
    "clientCertificate": "<clientCertificate>",
    "clientCertificateStoreLocation": "<clientCertificateStoreLocation>",
    "additionallyAllowedTenants": "<tenant-ids-separated-by-semicolon>"
}

注意

clientCertificateStoreLocationadditionallyAllowedTenants 键值对是可选的。 如果键存在且具有空值,则会忽略它们。 如果未指定 clientCertificateStoreLocation,则会使用 X509Credentials.StoreLocation 枚举中的默认 CurrentUser

创建 DefaultAzureCredential 类型

将以下键值对添加到 appsettings.json 文件以创建 Azure.Identity.DefaultAzureCredential

{
    "tenantId":  "<tenantId>",
    "clientId":  "<clientId>",
    "managedIdentityResourceId": "<managedIdentityResourceId>"
}