连接到 Azure 中国的 KeyVault

Connect to KeyVault in Azure China

我将此代码用于 Azure KeyVault 服务,它适用于常规 Azure

ClientSecretCredential clientCredential = new(options.KeyVaultTenantId, options.KeyVaultClientId, options.KeyVaultClientSecret);
KeyVaultCertificateWithPolicy rootCertificate = new CertificateClient(vaultUri, _clientCredential).GetCertificate(_rootCertificateId).Value;

但是我需要连接到 Azure 中国。代码失败并显示消息:

AADSTS90002: Tenant 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' not found. Check to make sure you have the correct tenant ID and are signing into the correct cloud. Check with your subscription administrator, this may happen if there are no active subscriptions for the tenant

我找到了 this,但我不知道在我的代码中在哪里设置 ActiveDirectoryServiceSettings.AzureChina。请推荐

<PackageReference Include="Azure.Identity" Version="1.5.0" />
<PackageReference Include="Azure.Security.KeyVault.Certificates" Version="4.2.0" />
<PackageReference Include="Azure.Security.KeyVault.Keys" Version="4.2.0" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.2.0" />

您需要使用 ClientSecretCredential(String, String, String, ClientSecretCredentialOptions) constructor override and set the AuthorityHost to Azure China in ClientSecretCredentialOptions

类似于:

var clientSecretCredentialOptions = new ClientSecretCredentialOptions()
{
    AuthorityHost =  AzureAuthorityHosts.AzureChina
};

ClientSecretCredential clientCredential = new(options.KeyVaultTenantId, options.KeyVaultClientId, options.KeyVaultClientSecret, clientSecretCredentialOptions);
KeyVaultCertificateWithPolicy rootCertificate = new CertificateClient(vaultUri, _clientCredential).GetCertificate(_rootCertificateId).Value;