使用服务主体和证书嵌入 Power BI 内容

基于证书的身份验证让你可以使用客户端证书通过 Microsoft Entra ID 进行身份验证。 客户端证书可以保存在 Windows、Android 或 iOS 设备上,也可以保存在 Azure 密钥保管库中。

借助这种身份验证方法,可使用证书颁发机构 (CA) 从一个中心位置管理证书,以便进行轮换或吊销。

要详细了解 Microsoft Entra ID 中的证书,请参阅客户端凭据流 GitHub 页面。

方法

  1. 使用服务主体嵌入内容

  2. 创建证书

  3. 设置证书身份验证

  4. 从 Azure Key Vault 获取证书

  5. 使用服务主体和证书进行身份验证

第 1 步 - 使用服务主体嵌入内容

若要使用服务主体嵌入内容,请按照使用服务主体和应用程序机密嵌入 Power BI 内容中的说明进行操作。

注意

如果你已有使用服务主体嵌入的内容,请跳过此步骤并前往第 2 步

第 2 步 - 创建证书

可以从受信任的证书颁发机构获取证书,也可以自行生成证书。

本部分介绍如何使用 Azure Key Vault 创建证书,以及下载包含公钥的 .cer 文件。

  1. 登录 Microsoft Azure

  2. 搜索并选择“密钥保管库”链接。

    Screenshot of the Azure portal window, which shows a link to the key vault service in the Services list.

  3. 选择要向其添加证书的密钥保管库。

    Screenshot of the Azure portal window, which shows a list of blurred out key vaults in the Key vaults list.

  4. 选择证书

    Screenshot of the Azure portal window, which shows the Key vaults page with the highlighted Certificates item.

  5. 选择“生成/导入”。

    Screenshot of the Azure portal window, which shows the Certificate pane with the highlighted Generate / Import item.

  6. 按如下所示配置“创建证书”字段:

    • 证书创建方法 - 生成

    • 证书名称 - 输入证书名称

    • 证书颁发机构 (CA) 类型 - 自签名证书

    • 使用者 - X.500 可分辨名称

    • DNS 名称 - 0 DNS 名称

    • 有效期(月) - 输入证书的有效期

    • 内容类型 - PKCS #12

    • 生存期操作类型 - 在达到给定生存期百分比时自动续订

    • 生存期百分比 - 80

    • 高级策略配置 - 未配置

  7. 选择“创建” 。 新创建的证书默认处于禁用状态。 可能需要长达五分钟的时间才会启用。

  8. 选择你创建的证书。

  9. 选择“以 CER 格式下载”。 下载的文件包含公钥。

    Screenshot of the Azure portal window, which shows the highlighted Download in CER Format button.

第 3 步 - 设置证书身份验证

  1. 在 Microsoft Entra 应用程序中,选择“证书和机密”选项卡

    Screenshot of the Azure portal window, which shows the certificates and secrets pane for an app.

  2. 选择“上传证书”,上传你在本教程的第 2 步中创建并下载的 .cer 文件。 .cer 文件包含公钥。

第 4 步 - 从 Azure 密钥保管库获取证书

使用托管服务标识 (MSI) 从 Azure 密钥保管库获取证书。 此过程涉及获取同时包含公钥和私钥的 .pfx 证书。

请参阅从 Azure 密钥保管库读取证书的代码示例。 如果要使用 Visual Studio,请参阅将 Visual Studio 配置为使用 MSI

private X509Certificate2 ReadCertificateFromVault(string certName)
{
    var serviceTokenProvider = new AzureServiceTokenProvider();
    var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(serviceTokenProvider.KeyVaultTokenCallback));
    CertificateBundle certificate = null;
    SecretBundle secret = null;

    certificate = keyVaultClient.GetCertificateAsync($"https://{KeyVaultName}.vault.azure.net/", certName).Result;
    secret = keyVaultClient.GetSecretAsync(certificate.SecretIdentifier.Identifier).Result;
    
    return new X509Certificate2(Convert.FromBase64String(secret.Value));
}

第 5 步 - 使用服务主体和证书进行身份验证

通过连接到 Azure 密钥保管库,可以使用服务主体和 Azure 密钥保管库中存储的证书对应用进行身份验证。

要连接并从 Azure 密钥保管库读取证书,请参阅下面的代码示例。

注意

如果你已有组织创建的证书,请将 .pfx 文件上传到 Azure 密钥保管库。

// Preparing needed variables
var Scope = "https://analysis.windows.net/powerbi/api/.default"
var ApplicationId = "{YourApplicationId}"
var tenantSpecificURL = "https://login.microsoftonline.com/{YourTenantId}/"
X509Certificate2 certificate = ReadCertificateFromVault(CertificateName);

// Authenticating with a SP and a certificate
public async Task<AuthenticationResult> DoAuthentication(){
    IConfidentialClientApplication clientApp = null;
    clientApp = ConfidentialClientApplicationBuilder.Create(ApplicationId)
                                                    .WithCertificate(certificate)
                                                    .WithAuthority(tenantSpecificURL)
                                                    .Build();
    return await clientApp.AcquireTokenForClient(Scope).ExecuteAsync();
}

将 Visual Studio 配置为使用 MSI

创建嵌入式解决方案时,将 Visual Studio 配置为使用托管服务标识 (MSI) 可能会很有用。 MSI 是用于管理 Microsoft Entra 标识的一项功能。 配置后,它将允许 Visual Studio 针对你的 Azure 密钥保管库进行身份验证。

注意

登录 Visual Studio 的用户需要拥有 Azure 密钥保管库权限才能获取证书。

  1. 在 Visual Studio 中打开项目。

  2. 选择“工具”>“选项”。

    Screenshot of the Visual Studio window, which shows the highlighted Options button in the Tools menu.

  3. 搜索并选择“帐户选择”。

    Screenshot of the Visual Studio Options window, which shows the highlighted Account Selection option in the search results.

  4. 添加有权访问你 Azure 密钥保管库的帐户。