Problems with ServerCertificateValidationCallback

Dean Cleaver <[email protected]> Mon, 23 Nov 2009 10:46:34 +1300
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <8E588B65FD82DF448592A3219102C3957E524E21AA@SXSLAKL001.xceptionsoftware.com>
I'm using the below code (trimmed a bit of unimportant stuff out) to check =
a given page on a site, and validate the server certificates. However, it s=
eems to not call the callback at all for some sites, despite the url being =
https, and manual checking of the site indicates a valid certificate.

I'm using an instance level function and a mutex so I can ensure that I'm c=
hecking one at a time. I've even stepped through the code, and the callback=
 just doesn't get called for some sites.

Any ideas why?

Dino


        private bool checkedCertificate;
        public void RunTest(object url)
        {
            Mutex mutex =3D new Mutex(false, "WebsiteLock");
            mutex.WaitOne();

            try
            {
                HttpWebRequest httpWebRequest =3D (HttpWebRequest) WebReque=
st.Create(url.ToString());
                ServicePointManager.ServerCertificateValidationCallback =3D=
 this.ValidateServerCertificate;
               =20
                checkedCertificate =3D false;

                HttpWebResponse httpWebResponse =3D (HttpWebResponse) httpW=
ebRequest.GetResponse();
            }
            finally
            {
                mutex.ReleaseMutex();
            }
        }

        public bool ValidateServerCertificate(
              object sender,
              X509Certificate certificate,
              X509Chain chain,
              SslPolicyErrors sslPolicyErrors)
        {
            checkedCertificate =3D true;

            // Allow this client to communicate with unauthenticated server=
s
            return true;
        }
    }
}

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives