Can't connect to HTTPS using X509 client certificate

穿精又带淫゛_ 提交于 2019-12-10 19:53:13

问题


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. The web service requires a client certificate - which I think I've installed correctly.

They have supplied me with a .PFX file. In Windows 7, I double clicked the file to install it into my Current User - Personal certificate store.

I then exported a X509 Base-64 encoded .cer file from the certificate entry in the store. 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.

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.

Thanks!


回答1:


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. I believe the Import() method can accept a .pfx file as input; the exported .cer file lacks the private key and isn't useful for client authentication.




回答2:


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



来源:https://stackoverflow.com/questions/2901105/cant-connect-to-https-using-x509-client-certificate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!