If your application accesses a webservice or other type of service where a secure connection is required by the use of SSL, you need to make sure this SSL certificate is valid. On a development machine this is not always possible. At runtime the application will throw an exception: System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
One way to solve this, is to handle the ServerCertificateValidationCallback function yourself by always returning true. Add the following code to your application start code like global.asax.cs.
protected void Application_BeginRequest(object sender, EventArgs e)
{
bool useUnsecureSSL = false;
Boolean.TryParse(ConfigurationManager.AppSettings["UseUnsecureSSL"] ?? "false", out useUnsecureSSL);
if (useUnsecureSSL)
{
ServicePointManager.ServerCertificateValidationCallback = delegate(object source, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};
}
}
Add an appSetting called useUnsecureSSL with a value of true to allow for a nonvalid SSL certificate. By default your application requires a valid certificate as it should in a production environment.
by Aidan
Monday, August 2nd, 2010 05:06 pm GMT +1 at 17:06
Hi Micheil,
Thanks for posting this – it’s been very useful as a starting point to working out why our client isn’t accepting the the server’s certificate.
I added some diagnostic code to the method body to come up with this:
ServicePointManager.ServerCertificateValidationCallback = delegate( object source, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors )
{
if( “None” == sslPolicyErrors.ToString() ) return true;
txtOutput.AppendText(“ServerCertificateValidation failed with error: ” + sslPolicyErrors + “\r\n”);
foreach( X509ChainElement cert in chain.ChainElements )
{
txtOutput.AppendText( cert.Certificate.SubjectName.Name+”\r\n” );
foreach( X509ChainStatus status in cert.ChainElementStatus )
txtOutput.AppendText( “\t” + status.Status + “\r\n” );
}
return true;
};