gpt4 book ai didi

c# - 400 未发送所需的 SSL 证书

转载 作者:太空宇宙 更新时间:2023-11-03 13:38:20 26 4
gpt4 key购买 nike

无法从 C# 客户端发送 https 请求。这是我的代码:

var client = WebRequest.Create("https://hastname.com/bla bla bla") as HttpWebRequest;
var cert = new X509Certificate2(File.ReadAllBytes("c:\\certs\\MyCert.pfx"), "MyPassword");
client.ClientCertificates.Add(cert);
result = new StreamReader(client.GetResponse().GetResponseStream()).ReadToEnd();

服务器端没问题,因为如果我在我的 mashine 证书商店中安装了证书,一切都很好。

我正在比较 2 个请求:1. 证书从证书库中删除,我用代码从文件中加载它2. 证书安装在证书库中

在我的结果下方:

首先,我从文件加载证书

Secure Protocol: Tls
Cipher: Aes256 256bits
Hash Algorithm: Sha1 160bits
Key Exchange: ECDHE_RSA (0xae06) 256bits

== Server Certificate ==========
[Subject]
CN=xxx, OU=Web Services, O=xxx, S=England, C=GB

[Issuer]
E=xx@xxx.com, CN=xxx Server Intermediate CA, OU=CA, O=xxx, S=England, C=GB

[Serial Number]
1000

[Not Before]
17.09.2015 12:51:29

[Not After]
26.09.2016 12:51:29

[Thumbprint]
xxxxxxxxxxxxxxxxxxxxxxxx7

然后我从本地 mashine 证书商店加载证书

Secure Protocol: Tls
Cipher: Aes256 256bits
Hash Algorithm: Sha1 160bits
Key Exchange: ECDHE_RSA (0xae06) 256bits

== Client Certificate ==========
[Subject]
CN=WinClient, OU=Client Apps, O=xxx, S=England, C=GB

[Issuer]
E=xx@xxxxx.com, CN=xxxx Client Intermediate CA, OU=CA, O=xxx, S=England, C=GB

[Serial Number]
1000

[Not Before]
17.09.2015 13:19:55

[Not After]
26.09.2016 13:19:55

[Thumbprint]
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7


== Server Certificate ==========
[Subject]
CN=xx.xxxxxxx.com, OU=Web Services, O=xxx, S=England, C=GB

[Issuer]
E=xx@xxxxx.com, CN=xxx Server Intermediate CA, OU=CA, O=xxx, S=England, C=GB

[Serial Number]
1000

[Not Before]
17.09.2015 12:51:29

[Not After]
26.09.2016 12:51:29

[Thumbprint]
xxxxxxxxxxxxxxxxxxxxxx7

如您所见,第一个请求中没有客户端证书部分。我该如何解决我的问题?

附注我的观点是发送带有从文件加载的证书的请求,而不是来自证书存储。

最佳答案

好的,首先,从字节数组创建 X509Certificate2 对象存在一些已知问题,请参阅此处的技巧 5 http://paulstovell.com/blog/x509certificate2了解更多信息。

.pfx 文件可以包含多个证书,因此您需要加载它并遍历以找到您想要的证书,或者只添加它们。试试这段代码

X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Import("c:\\certs\\MyCert.pfx", "MyPassword", X509KeyStorageFlags.PersistKeySet);
client.ClientCertificates.AddRange(collection);

关于c# - 400 未发送所需的 SSL 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32734573/

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