Call to TLS 1.2 server with RestSharp works in console app, not in Xamarin Forms

I am communicating with a server that uses mutual TLS v1.2. I have been able to make this request in a c# console application, but when I copy this exact code to Xamarin forms, my RestClient gives me an exception: "Connection reset by peer" after a few seconds.

The code:

var client = new RestClient("https://api-sandbox.rabobank.nl/openapi/sandbox/payments/account-information/ais/v3/accounts");

        ServicePointManager.Expect100Continue = true;
        ServicePointManager.DefaultConnectionLimit = 9999;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

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

        var request = new RestRequest(Method.GET);
        var response = client.Execute(request);
        Console.WriteLine(response.StatusCode);
        Console.WriteLine(response.Content);

I am unable to share my API keys and such, for obvious reasons. These are added through headers and are removed from the code snippet.

Things I have tried:

  • I checked if the projects are set to use TLS 1.2, both iOS and Android are.
  • I tried this in simulators and on real devices
  • I tried using this library, but whenever I try to add the certificate to the clientcertificate list of the handler, I get a NotImplementedException thrown

Answers

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    @Luca.9439

    It's hard to tell reason with such limited info.
    I think it may be caused by the certificate. And what's the version of RestSharp are you using? Is it the latest one?
    It's better you could provide a demo which can reproduce the issue. Then we can help you solve this problem.

  • Luca.9439Luca.9439 USMember ✭✭

    @BillyLiu said:
    @Luca.9439

    It's hard to tell reason with such limited info.
    I think it may be caused by the certificate. And what's the version of RestSharp are you using? Is it the latest one?
    It's better you could provide a demo which can reproduce the issue. Then we can help you solve this problem.

    I don't think it's caused by the certificate, because it works perfectly fine in a console application. I'm using the latest version of RestSharp btw.

    I did some more digging, and it seems to be something inside of mono. Since it works in .NET (the console app), but not in the mobile app (mono). There is a property on HttpWebRequest that is called version, you can set this one to one of the options from the HttpVersion enum. But this enum only contains version 10 and 11, not 12. I'm not sure if RestSharp uses the HttpWebRequest class, but if I cant use version 12, they shouldn't be able to either. So I think the issue is somewhere there.

    If you want, you can add me on skype or discord so I can send some working code. (PM for the contact details)

Sign In or Register to comment.