Search code examples
c#restsharp

Where to add host name while adding pfx certificate in RestSharp using C#?


I'm trying to communicate with a server where I need to pass pfx cert, host and passphrase.

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

enter image description here

I see there is a way to add pfx cert with password but how to add all 3 (host along with pfx cert and password)?

var client = new RestClient(url);
string certificatePath = @"certificates/certificate.pfx";
string pass = "password";

X509Certificate2 certificate = new X509Certificate2(certificatePath, pass);
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");

IRestResponse response = client.Execute(restrequest);

Solution

  • In PostMan, you configure the host so it doesn't use that certificate for communication with any host asking for a client certificate.

    In your code, you can ensure that you use the RestClient that you configure with that certificate only for communication with a given host.