soren.enemaerke
soren.enemaerke

Reputation: 4830

Validate certificate for single HttpWebRequest

I'm developing a utility library that is to be integrated into potentially quite large applications. One of the purposes of this library is to contact a central server using https communication. I would like to handle the ssl certificate validation for this call (potentially just accepting any certificate from this central server) but I ONLY want to do validating for this single request.

As far as I can tell there is a static validation callback for such validation:

ServicePointManager.ServerCertificateValidationCallback

I could explicitly specify a delegate from my utility library or hookup an event to the delegate, but this essentially hooks my into the validation process for the entire application and I really only want to validate the certificate for my specific HttpWebRequest.

Is there a way to hook into the certificate validation pipeline for only my specific request?

Upvotes: 0

Views: 1234

Answers (2)

Jason
Jason

Reputation: 81

This is an old question, now it is possible to set ServerCertificateValidationCallback on the HttpWebRequest:

request.ServerCertificateValidationCallback += 
    (sender, certificate, chain, sslPolicyErrors) => true;

Upvotes: 1

feroze
feroze

Reputation: 7594

You can create a new appdomain from your library. then, inside that appdomain, launch a request to the target server using the custom certification callback that you have. This will make sure that other requests running in other appdomains do not get your cert callback implementation.

Upvotes: 1

Related Questions