Tuesday, March 22, 2005

The underlying connection was closed: Could not establish trust relationship with remote server

Hello all. This week I have been coding extensively with the new .NET Link for ArcIMS. This is the native .NET connector that ESRI has shipped with ArcIMS 9. It seems to work great and has been much, much faster than the Active X connector. One problem I did run into was the error "The underlying connection was closed: Could not establish trust relationship with remote server". This error occured when I was using HTTPS as my scheme for connection to a remote (HTTPS) ArcIMS server.


The cause of this error is that my code doesn't like the credentials sent by the server in its HTTPS certificate. Help is on the way! The way around this error is to write your own certificate policy. Add this class to the class you are using to send the HTTPS request.


Public Class TrustAllCertificatePolicy
Implements System.Net.ICertificatePolicy
Public Sub TrustAllCertificatePolicy()
'Do Nothing
End Sub
Public Function CheckValidationResult(ByVal srvPoint As System.Net.ServicePoint, ByVal certificate As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal request As System.Net.WebRequest, ByVal certificateProblem As Integer) As Boolean Implements System.Net.ICertificatePolicy.CheckValidationResult
Return True
End Function
End Class
>


Then, make yourself a new certificate policy some time in the lifecycle of the page before you make the call. A good place for this call is in the page.load or in the constructor of your class, if you are going the three tier route.


System.Net.ServicePointManager.CertificatePolicy = New TrustAllCertificatePolicy


That's it! No more unhappy code.