【问题标题】:Unable to access private keycertificate from Azure function app无法从 Azure 函数应用访问私钥证书
【发布时间】:2020-11-24 14:09:00
【问题描述】:

我需要从需要客户端证书的 azure 函数应用调用 REST API。 我按照这个How to manage signed certificates with Azure Function V2 做了以下步骤:-

1)我已在私钥证书下的 TLS/SSL 设置中上传了我的私钥证书 (.PFX)。

2)在配置/应用程序设置下添加了一个键

WEBSITE_LOAD_CERTIFICATES:“我的证书指纹”

然后我尝试使用这个访问我的代码中的证书

使用系统; 使用 System.Security.Cryptography.X509Certificates;

...
X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection = certStore.Certificates.Find(
                            X509FindType.FindByThumbprint,
                            // Replace below with your certificate's thumbprint
                            "000000000000000000000000000000000000000",
                            false);
// Get the first cert with the thumbprint
if (certCollection.Count > 0)
{
    X509Certificate2 cert = certCollection[0];
    // Use certificate
    Console.WriteLine(cert.FriendlyName);
}
certStore.Close();
...

我没有取回任何证书。我究竟做错了什么? 如果我上传公共证书 (.cer),我可以访问该证书,但它没有私钥,因此我无法调用该服务。

【问题讨论】:

    标签: azure azure-functions


    【解决方案1】:

    首先,请检查您是否已将Thumbprint(在下面的屏幕截图中)复制到您的功能应用程序设置WEBSITE_LOAD_CERTIFICATES

    然后请在您的代码中使用cert 的一些其他字段进行测试,因为有时证书没有FriendlyName。我在我身边进行测试,它对FriendlyName 没有任何显示。然后我用Issuer 测试,它工作正常。(注意我使用log.LogInformation(cert.Issuer); 而不是Console.WriteLine(cert.Issuer);,因为我发现Console.WriteLine 什么都没有显示)

    【讨论】:

    • 如果我上传 .pfx 文件,证书计数为零。问题不在于日志记录。如果我上传 .cer 文件,我可以获得证书。
    • 嗨@user911 我在我身边测试它,发现如果我上传一个.cer证书,即使我没有在应用程序中设置WEBSITE_LOAD_CERTIFICATESThumbprint,我也可以获得证书功能设置(如果我在我的功能代码中使用硬代码Thumbprint)。但是如果我上传.pfx证书,即使我在功能代码中使用硬代码Thumbprint,也需要在应用程序设置中将WEBSITE_LOAD_CERTIFICATES设置为Thumbprint。所以请检查您是否正确设置了WEBSITE_LOAD_CERTIFICATES,即使您在函数中使用硬编码Thumbprint
    • @user911 如果还是同样的问题,请更换另一个.pfx证书进行测试。