Sunday, November 11, 2012

SSL problems with Microsoft Azure Mobile Services on Mono for Android (MonoDroid)

My main test Android device - a SIM-unlocked Galaxy Note upgraded to ICS - doesn't like any 'Microsoft Secure Server Authority' certificates. e.g. it doesn't like Hotmail.com or Live.com.

This is a problem if you try to use Azure Mobile Services - because https connections to a service like https://mydemoservice.azure-mobile.net will result in an AggregateException within which (several layers deep) will be a MobileServiceInvalidOperationException with a message saying just 'Send Failure' (derived from ServiceFilterResponseStatus SendFailure)

If anyone else encounters this issue... then below is some hacky code I transplanted from an old demo project. It seems to work.... but could probably be improved to work with just Microsoft Secure Server Authority messages.

This code hacks the ServicePointManager to allow it to accept all HTTPS/SSL addresses regardless of whether or not the certificate is trusted. Don't use this in your banking apps please :)

// this code is a workaround for:
// "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."
// - this error caused by Nike not having a valid certificate now!
ServicePointManager.ServerCertificateValidationCallback = delegate
(object sender,
System.Security.Cryptography.X509Certificates.X509Certificate pCertificate,
System.Security.Cryptography.X509Certificates.X509Chain pChain,
System.Net.Security.SslPolicyErrors pSSLPolicyErrors)
{
//if (pSSLPolicyErrors == System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch && pCertificate.Issuer == "CN=Entrust Certification Authority - L1C, OU=\"(c) 2009 Entrust, Inc.\", OU=www.entrust.net/rpa is incorporated by reference, O=\"Entrust, Inc.\", C=US")
{
return true;
}
//if (pSSLPolicyErrors == System.Net.Security.SslPolicyErrors.None)
// return true;
//return false;
};
view raw gistfile1.txt hosted with ❤ by GitHub

No comments:

Post a Comment