0

I am trying to call the API, That API required Certificate and the public key to authenticate the request.

Its working fine when I am attaching the certificate in postman and calling the API

But when I am doing the same through Rest API request its not working. I think I am not able to attach the certificate properly in my rest request.

enter image description here

Here is my code.

var client = new RestClient("https://xyz.in");


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

var request = new RestRequest("/api/test", Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "application/json");
var certFile = "test.pfx";
X509Certificate2 certificate = new X509Certificate2(certFile, "test12");
client.ClientCertificates = new X509CertificateCollection() { certificate };
// client.Proxy = new WebProxy();
string body = "My test contend"
request.AddParameter("application/json; charset=utf-8", body, ParameterType.RequestBody);
var response = client.Execute(request);
Console.WriteLine(response.Content);

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#.

Pulkit
  • 23
  • 1
  • 6
  • Does this answer your question? [Add certificate on request with RestSharp](https://stackoverflow.com/questions/49069197/add-certificate-on-request-with-restsharp) – ɐsɹǝʌ ǝɔıʌ Jun 06 '23 at 11:14

0 Answers0