【问题标题】:How to RestSharp add client certificate in Https request? (C#)RestSharp 如何在 Https 请求中添加客户端证书? (C#)
【发布时间】:2019-08-22 03:57:46
【问题描述】:

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

    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;

    }

【问题讨论】:

  • 嗨!你得到了什么样的异常?

标签: c# ssl x509certificate restsharp


【解决方案1】:

首先你应该导入证书,然后附加到请求中

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

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

【讨论】:

  • 所以这确实有效,但是当我发送请求时,证书不在上面。尽管验证客户端证书集合填充了我的 -self-signed-cert
  • 好的,我确实这样做了,现在我正在通过网络发送证书。感谢您的编辑。您是否知道在 WebAPI 中接受客户端证书的最低要求?
猜你喜欢
  • 2018-08-15
  • 2013-11-20
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-02
  • 2023-03-17
相关资源
最近更新 更多