Re: Problems with ServerCertificateValidationCallback

Dean Cleaver <[email protected]> Tue, 24 Nov 2009 08:30:42 +1300
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <8E588B65FD82DF448592A3219102C3957E524E21B2@SXSLAKL001.xceptionsoftware.com>
A friend helped me find another property (ServicePointManager.MaxServicePoi=
ntIdleTime) that seems to stop the ServicePointManager from caching the cer=
tificate data.

Dino

-----Original Message-----
From: Discussion of advanced .NET topics. [mailto:[email protected]=
E.LSOFT.COM] On Behalf Of Ryan Heath
Sent: Monday, 23 November 2009 8:50 p.m.
To: [email protected]
Subject: Re: [ADVANCED-DOTNET] Problems with ServerCertificateValidationCal=
lback

Just a uneducated guess:
what happens when you set
ServicePointManager.ServerCertificateValidationCallback
=3D this.ValidateServerCertificate;
before the HttpWebRequest httpWebRequest =3D (HttpWebRequest) WebRequest.Cr=
eate(url.ToString()); ?

From your explanation is seems possible that the callback is set for the ne=
xt webrequest.create?

// Ryan

On Sun, Nov 22, 2009 at 11:42 PM, Dean Cleaver < dean.cleaver@xceptionsoftw=
are.com> wrote:

> No - that's what I mean by the callback failing - it doesn't get called.
>
> I have a sample where I'm hitting a page on my server. I click the=20
> button, the callback is called, and then it exits the test function. I=20
> click the button again, and it exits the test function without hitting=20
> the ValidateServerCertificate.
>
> I've tried almost every option for CachePolicy I can see (BypassCache,=20
> Reload, NoCacheNoStore) and I still get the same behaviour - it skips=20
> the certificate validation on subsequent calls, however in the case of=20
> my real application where it's testing multiple sites in multiple=20
> threads, it skips one or 2 at random, even on the first call to the serve=
r.
>
> Dino
>
> -----Original Message-----
> From: Discussion of advanced .NET topics. [mailto:
> [email protected]] On Behalf Of John Warner
> Sent: Monday, 23 November 2009 11:13 a.m.
> To: [email protected]
> Subject: Re: [ADVANCED-DOTNET] Problems with=20
> ServerCertificateValidationCallback
>
> Does ValidateServerCertificate get called when the callback fails?
>
> John Warner
>
>
> > -----Original Message-----
> > From: Discussion of advanced .NET topics. [mailto:ADVANCED-=20
> > [email protected]] On Behalf Of Dean Cleaver
> > Sent: Sunday, November 22, 2009 4:47 PM
> > To: [email protected]
> > Subject: [ADVANCED-DOTNET] Problems with=20
> > ServerCertificateValidationCallback
> >
> > 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,=20
> > it
> seems 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=20
> > that
> I'm
> > checking 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)=20
> > WebRequest.Create(url.ToString());
> >                =20
> > ServicePointManager.ServerCertificateValidationCallback
> =3D
> > this.ValidateServerCertificate;
> >
> >                 checkedCertificate =3D false;
> >
> >                 HttpWebResponse httpWebResponse =3D (HttpWebResponse)=20
> > httpWebRequest.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
> servers
> >             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=20
> > http://peach.ease.lsoft.com/archives
>
> =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=20
> http://peach.ease.lsoft.com/archives
>
> =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=20
> http://peach.ease.lsoft.com/archives
>

=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.co=
m/archives

=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