简体   繁体   English

无法使用X509客户端证书连接到HTTPS

[英]Can't connect to HTTPS using X509 client certificate

I'm new to cryptography and I'm a bit stuck: 我是密码学的新手,我有点卡住了:

I'm trying to connect (from my development environment) to a web service using HTTPS. 我正在尝试使用HTTPS连接(从我的开发环境)到Web服务。 The web service requires a client certificate - which I think I've installed correctly. Web服务需要客户端证书 - 我认为我已正确安装。

They have supplied me with a .PFX file. 他们为我提供了一个.PFX文件。 In Windows 7, I double clicked the file to install it into my Current User - Personal certificate store. 在Windows 7中,我双击该文件以将其安装到我的当前用户 - 个人证书存储区中。

I then exported a X509 Base-64 encoded .cer file from the certificate entry in the store. 然后,我从商店的证书条目中导出了X509 Base-64编码的.cer文件。 It didn't have a private key associate with it. 它没有私钥关联。

Then, in my app, I'm attempting to connect to the service like this: 然后,在我的应用程序中,我正在尝试连接到这样的服务:

var certificate = X509Certificate.CreateFromCertFile("xyz.cer"));
var serviceUrl = "https://xyz";
var request = (HttpWebRequest) WebRequest.Create(serviceUrl);
request.ClientCertificates.Add(certificate);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";

I get a 502 Connection failed when I connect. 我连接时遇到502连接失败。

Is there anything you can see wrong with this method? 这个方法有什么问题吗? Our production environment seems to work with a similar configuration, but it's running Windows Server 2003. 我们的生产环境似乎使用类似的配置,但它运行的是Windows Server 2003。

Thanks! 谢谢!

The underlying problem is that you are only giving your program access to the certificate. 根本问题是您只能让您的程序访问证书。 To perform authentication, it needs access to the private key too. 要执行身份验证,它还需要访问私钥。

A correctly instantiated X509Certificate2 can carry the private key, and should be passed to ClientCertificates.Add() method. 正确实例化的X509Certificate2可以携带私钥,并且应该传递给ClientCertificates.Add()方法。 I believe the Import() method can accept a .pfx file as input; 我相信Import()方法可以接受.pfx文件作为输入; the exported .cer file lacks the private key and isn't useful for client authentication. 导出的.cer文件缺少私钥,对客户端身份验证没有用。

Take a look at this and verify that your error is due to a certificate - is the server sending back more details on the problem? 看看这个,并验证您的错误是由证书引起的 - 服务器是否发送了有关该问题的更多详细信息? Error 502 (Bad Gateway) when sending a request with HttpWebRequest over SSL 通过SSL发送带有HttpWebRequest的请求时出现错误502(错误网关)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM