根据要求使用RestSharp添加证书 [英] Add certificate on request with RestSharp

查看:983
本文介绍了根据要求使用RestSharp添加证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与服务器通信。该服务器向我发送了证书和私钥,以便成功执行我的请求。



要测试服务器,请使用



现在我想在C#中做同样的事情。



为此,我使用 RestSharp 即可创建请求。



这是我的代码

  var client = new RestClient(url ); 

byte [] certBuffer = UtilsService.GetBytesFromPEM(myCertificate,Models.Enum.PemStringType.Certificate);
byte [] keyBuffer = UtilsService.GetBytesFromPEM(encryptedPrivateKey,Models.Enum.PemStringType.RsaPrivateKey);

X509Certificate2证书=新的X509Certificate2(certBuffer,secret);
client.ClientCertificates = new X509CertificateCollection(){certificate};
var request = new RestRequest(Method.POST);
request.AddHeader( Cache-Control, no-cache);
request.AddHeader( Accept, application / json);
request.AddHeader( Content-Type, application / json);
request.AddParameter( myStuff,ParameterType.RequestBody);
IRestResponse响应= client.Execute(请求);

该请求无效。我认为问题出在我如何在RestSharp中加载证书。



我正在寻找有关如何在RestSharp中正确设置证书的信息。



我正在使用RestSharp ,但我可以是其他任何可以在C#中工作的东西。

解决方案

好,我得到了解决方案。



首先,我必须停止使用.crt和.key作为证书。我必须得到一个.pfx。可以使用openssl命令( openssl文档)完成 p>

  openssl pkcs12-导出-out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt 

创建证书后,只需将其添加到请求中即可

  var client = new RestClient(url); 

ServicePointManager.Expect100Continue = true;
ServicePointManager.DefaultConnectionLimit = 9999;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;

var certFile = Path.Combine(certificateFolder, certificate.pfx);
X509Certificate2证书=新的X509Certificate2(certFile,onboard.authentication.secret);
client.ClientCertificates = new X509CertificateCollection(){certificate};
client.Proxy = new WebProxy();
var restrequest = new RestRequest(Method.POST);
restrequest.AddHeader( Cache-Control, no-cache);
restrequest.AddHeader( Accept, application / json);
restrequest.AddHeader( Content-Type, application / json);
restrequest.AddParameter( myStuff,ParameterType.RequestBody);
IRestResponse响应= client.Execute(restrequest);


I'm trying to communicate with a server. This server send me a certificate and a private key in order to execute my request successfully.

To test the server, I use Postman. So I fill the certificate setting in postman, and my request works fine

Now I want to do the same in C#.

For that I use RestSharp in order to create the request.

Here is my code

 var client = new RestClient(url);

 byte[] certBuffer = UtilsService.GetBytesFromPEM(myCertificate, Models.Enum.PemStringType.Certificate);
 byte[] keyBuffer = UtilsService.GetBytesFromPEM(encryptedPrivateKey, Models.Enum.PemStringType.RsaPrivateKey);

 X509Certificate2 certificate = new X509Certificate2(certBuffer, secret);
 client.ClientCertificates = new X509CertificateCollection() { certificate };
 var request = new RestRequest(Method.POST);
 request.AddHeader("Cache-Control", "no-cache");
 request.AddHeader("Accept", "application/json");
 request.AddHeader("Content-Type", "application/json");
 request.AddParameter("myStuff", ParameterType.RequestBody);
 IRestResponse response = client.Execute(request);

The request doesn't work. I think the problem is from how I load the certificate in RestSharp.

I'm looking for information how to set correctly the certificate in RestSharp.

I'm using RestSharp, but I could be anything else that can work in C#

解决方案

Ok, I got the solution.

First of all, I had to stop using the .crt and the .key for the certificate. I have to get a .pfx. This can be done with openssl command (openssl documentation)

openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt

After creating the certificate, just add it to the request like this

var client = new RestClient(url);

ServicePointManager.Expect100Continue = true;
ServicePointManager.DefaultConnectionLimit = 9999;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;

var certFile = Path.Combine(certificateFolder, "certificate.pfx");
X509Certificate2 certificate = new X509Certificate2(certFile, onboard.authentication.secret);
client.ClientCertificates = new X509CertificateCollection() { certificate };
client.Proxy = new WebProxy();
var restrequest = new RestRequest(Method.POST);
restrequest.AddHeader("Cache-Control", "no-cache");
restrequest.AddHeader("Accept", "application/json");
restrequest.AddHeader("Content-Type", "application/json");
restrequest.AddParameter("myStuff", ParameterType.RequestBody);
IRestResponse response = client.Execute(restrequest);

这篇关于根据要求使用RestSharp添加证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆