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.