gpt4 book ai didi

c# - 无法使用证书存储中的客户端证书,只能通过从文件加载

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:16 25 4
gpt4 key购买 nike

我正在使用 WebRequest 连接到使用 https 的网页。

如果我尝试使用位于 LocalMachine 个人存储中的客户端证书

var myStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
myStore.Open(OpenFlags.ReadOnly);

var clientCertificates = myStore.Certificates.Find(X509FindType.FindBySubjectName, "foobar",
validOnly: false);

然后我创建一个连接

var req = (HttpWebRequest)WebRequest.Create("https://theserver.com/the/url");
req.Method = "POST";
req.ContentType = "application/json";
req.ProtocolVersion = HttpVersion.Version11;

req.ClientCertificates = clientCertificates;

req.ServerCertificateValidationCallback += (o, certificate, chain, errors) =>
{
return true;
};

using (var stream = req.GetRequestStream())
{
stream.Write(data, 0, data.Length);
stream.Flush();
}

var response = req.GetResponse();

我在 req.GetResponse 行上失败了

System.Net.WebException: 'The request was aborted: Could not create SSL/TLS secure channel.'

但是如果我改为从文件加载客户端证书(我之前在商店中安装的相同文件)

var certificatePath = @"C:\temp\file.p12";

var clientCertificate = new X509Certificate2(certificatePath, "pwd",
X509KeyStorageFlags.Exportable);

req.ClientCertificates.Add(clientCertificate);

我将能够成功运行 https 查询。

这需要我在文件系统中保留一个文件,并在源代码中放置一个密码。

  • 如何使用存储而不是文件系统?

  • 为什么我必须使用 validOnly: false 来获取客户端证书?

编辑

访问这些证书(客户端和服务器证书)的首选方法是将它们作为端点放在 web.config 中,但我没有成功这样做。

最佳答案

为了解决问题/许多问题的症状,我不得不将用户 Everyone 添加到证书私钥的 ACL 中。

关于c# - 无法使用证书存储中的客户端证书,只能通过从文件加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58170085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com