Azure 函数不断返回“无法识别提供给包的凭据”

时间:2021-03-02 12:02:55

标签: c# azure azure-functions tls1.2 client-certificates

我有一个 Azure 函数,它不断向我返回“无法识别提供给包的凭据”。证书(pfx 格式)已作为私钥证书上传到 Azure - 请看下图 - 状态良好,我可以读取其中的 3 个证书。

enter image description here

我还设置了 appSettings WEBSITE_LOAD_USER_PROFILE=1 和 WEBSITE_LOAD_CERTIFICATES=thumbprints 但没有运气。这是我为函数应用设置的应用程序。

enter image description here

这是我在处理证书时使用的代码。我正在关注 Microsoft 的官方文档 - https://docs.microsoft.com/da-dk/azure/app-service/configure-ssl-certificate-in-code#load-certificate-in-windows-apps

using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;

string certThumbprint = "certThumbprint";
bool validOnly = false;

using (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
                              certThumbprint,
                              validOnly);
  // Get the first cert with the thumbprint
  X509Certificate2 cert = certCollection.OfType<X509Certificate>().FirstOrDefault();

  if (cert is null)
      throw new Exception($"Certificate with thumbprint {certThumbprint} was not found");

  // Use certificate
  Console.WriteLine(cert.FriendlyName); 
}

1 个答案:

答案 0 :(得分:0)

总结作者提供的意见,供其他社区参考:

问题是由以下代码行引起的:X509Certificate2 cert = certCollection.OfType<X509Certificate>().FirstOrDefault();

将代码行改为:if (certCollection.Count > 0) { return certCollection[0]; },然后就可以了。

相关问题