使用 ClientCertificateCredential 对 azure 注册应用程序进行身份验证的基本场景返回“证书没有私钥”

问题描述 投票:0回答:1

我是 Azure 新手,正在尝试一个非常基本的方案:使用证书凭据对已注册的应用程序进行本地身份验证。

代码如下:

public async Task<QueueClient> GetQueueClientWithSdkWithCertificateCredentials(string storageAccountName, string queueName, string tenantId, string clientId, string certificateBase64Str)
{
    var certificateCredentials = new ClientCertificateCredential(tenantId, clientId, new X509Certificate2(Convert.FromBase64String(certificateBase64Str)));
    var queueClient = new QueueClient(new Uri($"https://{storageAccountName}.queue.core.windows.net/{queueName}"), certificateCredentials);
    var createQueueResponse = await queueClient.CreateAsync();
    if (createQueueResponse.IsError)
    {
        throw new Exception($"Error in creating queue: [{createQueueResponse}]");
    }
    return queueClient;
}

queueClient.CreateAsync
因错误而失败
Azure.Identity.AuthenticationFailedException: 'ClientCertificateCredential authentication failed: The certificate certificate does not have a private key.

我的设置是:

  1. 我已经使用 openssl 为自己创建了证书,如下所示:
    .\openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 120 -nodes -subj "/C=XX/ST=South/L=Sderot/O=CodeValue/OU=EPIL/CN=MyCertificate"
  2. 然后,我将证书 (cert.pem) 转换为 pfx 文件,以便可以在 Windows 上正确安装它(选中“导出私钥”复选框,以便在双击证书查看其详细信息时显示密钥图标)。
  3. 我还确保它安装在受信任的根证书颁发机构下,以防万一。
  4. 所以证书最终安装在我的 Windows 本地存储中,看起来像
  5. 然后,我将 cert.pem 文件上传到 Azure,位于我注册的应用程序的“证书和机密”屏幕下。
  6. 然后我转到注册应用程序的清单屏幕,并获取我应该在 Azure SDK API 中使用的证书基 64 表示形式(“value”属性)

执行所有这些操作 - 返回上述错误。 “证书证书没有私钥”是什么意思?我的意思是,本地安装的证书确实在底部显示了密钥 - 这表明它确实有私钥吗?

我缺少什么?

azure x509certificate2 azure-sdk-.net azure-sdk
1个回答
0
投票

创建Microsoft Entra ID 申请并上传证书:

enter image description here

导出本机中的PFX证书:

enter image description here

如果没有传递私钥,通常会出现错误“证书没有私钥”

要解决该错误,在使用客户端证书身份验证时传递私钥和证书路径。

修改如下代码以使用客户端证书身份验证创建队列:

namespace ConsoleApp1
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string storageAccountName = "testrukstrgacc";
            string queueName = "testrukqueue";
            string tenantId = "TenantID";
            string clientId = "ClientID";
            string certificatePath = "C:/demo/rukcert.pfx";
            string certificatePassword = "Trash33!";

            QueueClient queueClient = await GetQueueClientWithSdkWithCertificateCredentials(storageAccountName, queueName, tenantId, clientId, certificatePath, certificatePassword);

            Console.WriteLine($"Queue client created: {queueClient.Uri}");
        }

        public static async Task<QueueClient> GetQueueClientWithSdkWithCertificateCredentials(string storageAccountName, string queueName, string tenantId, string clientId, string certificatePath, string certificatePassword)
        {
            var certificate = new X509Certificate2(certificatePath, certificatePassword);

            var certificateCredentials = new ClientCertificateCredential(tenantId, clientId, certificate);
            var queueClient = new QueueClient(new Uri($"https://{storageAccountName}.queue.core.windows.net/{queueName}"), certificateCredentials);
            var createQueueResponse = await queueClient.CreateAsync();
            if (createQueueResponse.IsError)
            {
                throw new Exception($"Error in creating queue: [{createQueueResponse}]");
            }
            return queueClient;
        }
    }
}

队列创建成功:

enter image description here

在门户中:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.