Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to RestSharp add client certificate in Https request? (C#)

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;

    }
like image 855
Rabbit Avatar asked Dec 29 '15 07:12

Rabbit


People also ask

How do I enable client certificates?

On the taskbar, click Start, and then click Control Panel. In Control Panel, click Programs and Features, and then click Turn Windows Features on or off. Expand Internet Information Services, then select Client Certificate Mapping Authentication, and then click OK.


Video Answer


1 Answers

At first you should import certificate and then attach to request

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

client.ClientCertificates = new X509CertificateCollection(){certificate};
like image 58
tungula Avatar answered Sep 17 '22 12:09

tungula