16

How to RestSharp add client certificate in Https request ? My code it doesn't work .

    public static IRestResponse<User> AsyncHttpRequestLogIn(string path, string method, object obj)
    {
        var client = new RestClient(Constants.BASE_URL + path); // https:....
        var request = method.Equals("POST") ? new RestRequest(Method.POST) : new RestRequest(Method.GET);
        request.RequestFormat = RestSharp.DataFormat.Json;

        // The path to the certificate.
        string certificate = "cer/cert.cer";     

        client.ClientCertificates.Add(new X509Certificate(certificate));

        request.AddBody(
            obj
        );


        IRestResponse<User> response = client.Execute<User>(request);

        return response;

    }
Rabbit
  • 161
  • 1
  • 1
  • 5

1 Answers1

29

At first you should import certificate and then attach to request

X509Certificate2 certificate = new X509Certificate2();
certificates.Import(...);

client.ClientCertificates = new X509CertificateCollection(){certificate};
Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
tungula
  • 578
  • 1
  • 6
  • 12
  • So this did indeed work, however when I send the request the certificate is not on it. Despite verifying the clients certificate collection is populated with my -self-signed- cert – Chazt3n Mar 03 '16 at 22:51
  • OK I definitely did this, now I'm sending the certificate across the wire. Thanks for the edit. Do you happen to know the minimum requirements for accepting a client certificate in WebAPI? – Chazt3n Mar 07 '16 at 16:09