1

I have a Web-API server with some functionality that uses self-signed SSL certificate. I have a class library that is used for both windows and android apps.
The important things about this class library:

ServicePointManager.ServerCertificateValidationCallback += ValidateCert;

And the method itself:

private bool ValidateCert(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    var apiCertHash = new byte[]
    {118, 15, 13, 101, 224, 50, 146, 17, 66, 100, 153, 11, 98, 175, 102, 166, 111, 225, 105, 111};

    if (sslPolicyErrors == SslPolicyErrors.None)
    {
        // Good certificate.
        return true;
    }

    bool certMatch = false; // Assume failure
    byte[] certHash = certificate.GetCertHash();

    if (certHash.Length == apiCertHash.Length)
    {
        certMatch = !certHash.Where((t, idx) => t != apiCertHash[idx]).Any();
    }

    return certMatch;
}

All that works fine with my _httpClient.PostAsync() and _httpClient.GetAsync() BUT ONLY ON WINDOWS. On the other hand in Xamarin.Android I cannot access my server via HTTPS, all the requests just hang until TaskCancelledException is thrown. I can access my server through emulators' browser via HTTP and HTTPS (I do get certificate warnings) AND access it through HTTP from my app. While I try to use HTTPS in the Xamarin.Android app my ValidateCert is not called, the server does not see any requests at all. The app has all possible permissions.

HardLuck
  • 1,497
  • 1
  • 22
  • 43
  • Do you have the certificate installed on your device? – gmiley May 29 '16 at 11:52
  • I've installed it on the emulator as a trusted one but this didn't help – HardLuck May 29 '16 at 11:58
  • Can you run the code on your actual device? – gmiley May 29 '16 at 12:01
  • This may be related to: http://stackoverflow.com/questions/28847309/webrequest-not-working-with-ssl-on-xamarin-forms-despite-servicepointmanager-ha - You might need to use `HttpClient` instead of `HttpWebRequest`. Take a look at that Q/A and see if that helps. – gmiley May 29 '16 at 12:12
  • I'm using exactly HttpClient (tried both the stock one and ModernHttpClient NuGet) – HardLuck May 29 '16 at 12:52

0 Answers0