简体   繁体   中英

RestSharp not sending certificate but Postman will

I just can't see what I'm doing wrong. I have a certificate that I obtained (not self signed). I have a pfx file and a password. I added the certificate in Postman and the API call works. I try to add the same certificate using RestSharp and it fails - the server returns a 400 error with a message saying no SSL certificate was supplied.

 public void AttachCertificate(RestClient client)
        {
            try
            {
                
                X509Certificate2 cert = new X509Certificate2(@"C:\...\xxx.pfx", "-password-");
                if (client.Options.ClientCertificates == null) client.Options.ClientCertificates = new X509CertificateCollection();
                if (cert != null) client.Options.ClientCertificates.Add(cert);
            } catch(Exception ex)
            {
                var x = ex;
            }

       
        }

I can see the certificate is there (in RestClient.Options.ClientCertificates) in debugger after this code runs. This is in.Net Core 3.1

I figured it out by looking at the source for RestSharp RestClient. In the constructor for RestClient it configures the HttpMessageHandler (HttpClientHandler class) and adds any certificates from RestClient Options. But if you add the certificate to the Options object after the fact (as I was doing), it never goes on to update the underlying HTTP objects.

I'm sure this same issue impacts CookieContainer, AutomaticDecompression, Credentials, Proxy, FollowRedirects, PreAuthenticate, RemoteCertificateValidationCallback options on the HttpClientHandler object; and MaxTimeout, UserAgent and Expect100Continue options on the HttpClient.

IMHO either: setting any of these Options after the RestClient is constructed should cascade to the underlying objects, OR the Options object should be read-only. But that is a decision for the RestSharp developers...

I have just tested my code that I changed to get the PFX certificate BEFORE creating the RestClient, and setting it in the RestClient Options object as passed into the RestClient constructor - and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM