如何在Https请求中添加RestSharp添加客户端证书? (C#)

时间:2015-12-29 07:00:27

标签: c# ssl x509certificate restsharp

如何在Https请求中添加RestSharp添加客户端证书?我的代码没有用。

    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;

    }

1 个答案:

答案 0 :(得分:21)

首先,您应导入证书,然后附加到请求

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

client.ClientCertificates = new X509CertificateCollection(){certificate});