首页 > 解决方案 > 尝试连接时来自 Azure Vault 的 GetSecret 抛出 NullReferenceException

问题描述

我有以下问题:

我正在尝试使用证书凭据连接到 Azure Vault。

当试图阅读一个秘密时,我得到AuthenticationFailedException一个内部的NullReferenceException.

为简洁起见,我只包括 NullReferenceException 的堆栈跟踪:

This exception was originally thrown at this call stack:
    Azure.Identity.AadIdentityClient.CreateClientAssertionJWT(string, string, System.Security.Cryptography.X509Certificates.X509Certificate2)
    Azure.Identity.AadIdentityClient.CreateClientCertificateAuthRequest(string, string, System.Security.Cryptography.X509Certificates.X509Certificate2, string[])
    Azure.Identity.AadIdentityClient.Authenticate(string, string, System.Security.Cryptography.X509Certificates.X509Certificate2, string[], System.Threading.CancellationToken)
    Azure.Identity.ClientCertificateCredential.GetToken(Azure.Core.TokenRequestContext, System.Threading.CancellationToken)

编码:

            string keyVaultUrl = "https://somevault.vault.azure.net/";
            var clientId = "<Application (client) ID>";
            var tenantId = "<Directory (tenant) ID>";
            X509Certificate2 clientCertificate = new X509Certificate2(@"someCerFile.cer");
            ClientCertificateCredential certificateCreadential = new ClientCertificateCredential(tenantId, clientId, clientCertificate);
            var client = new SecretClient(new Uri(keyVaultUrl), certificateCreadential);
            var secret = client.GetSecret("Password"); //This line throws the exception

保险库配置是根据此处概述的步骤完成的:

https://docs.microsoft.com/en-us/azure/key-vault/general/authentication

有任何想法吗?

标签: c#.netazureazure-keyvault

解决方案


不幸的是,这个例外不是很有帮助。但是,我相信这里的问题是您需要给 X509Certificate2 提供私钥,以便ClientCertificateCredential可以签署客户端断言。该.cer文件只是证书的公共部分。您将需要提供一个.pfx 包含证书和私钥的文件。


推荐阅读