.net - 从函数应用服务的代码中加载证书

标签 .net azure ssl-certificate azure-functions

我在我的 azure 函数应用程序的 SSL 设置中导入了一个 .pfx 文件,如下所示:

enter image description here

我已在应用程序设置中将 WEBSITE_LOAD_CERTIFICATES 设置为“*”,这意味着我的应用程序会加载找到的任何证书。

enter image description here

最后在我的代码中我加载并使用证书,如下所示:

X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, "13865DB1**************33384B053E4B5", true);

log.LogInformation("Number of certificates in collection: " + certCollection.Count.ToString());

certStore.Close();

var handler = new HttpClientHandler();
handler.ClientCertificates.Add(certCollection[0]);

当函数运行时,日志行显示“集合中的证书数量:0”,并且 handler.ClientCertificates.Add(certCollection[0]); 给出索引越界异常。我在代码中使用证书的方式是否有问题,或者有什么原因导致它无法加载?我认为当 WEBSITE_LOAD_CERTIFICATES 为“*”时,它应该自动发生。

编辑:在 中从 true 切换到 false 时,它似乎有效 X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, "13865DB1****************33384B053E4B5", true);,但我不明白为什么。 bool 被称为 validOnly 所以我觉得 true 应该是正确的值,因为我正在寻找的证书是有效的......

最佳答案

如果你看看这个answer ,

如果 validOnly 设置为 true,该函数将调用 X509Utils.VerifyCertificate,进一步调用 native API CertVerifyCertificateChainPolicy 来验证证书。 CertVerifyCertificateChainPolicy 函数检查证书链以验证其有效性,包括其是否符合任何指定的有效性策略标准。

此外,我建议您添加 array.length 方法来检查证书的数量并访问第一个元素。

关于.net - 从函数应用服务的代码中加载证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59299845/

相关文章:

c# - 向字符串类添加扩展方法 - C#

c# - 初始化语法

.net - .NET 的硬件 AES 支持

azure - 在 Azure API Manager 中仅允许单个后端调用

ssl - 是否可以在 Windows Azure 上拥有一个具有多个域和 SSL 证书的 Web 角色?

.net - Azure 构建/发布管道将库推送到 nuget

ssl - 为什么我会收到验证错误 :unable to get local issuer certificate when I run openssl ocsp?

python-3.x - 如何禁用cloudscraper Python中的安全证书检查

c# - 无法显示链接字段值表+ReportViewer

Facebook 更新: How to provide an SSL certificate