Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore SSL certificate override check

Tags:

c#

I am writing a test to test a service I am deploying, to bypass the ssl cert check I implemented an ssl override using the snippet below:


public static void SSLValidationOverride()
        {
            ServicePointManager.ServerCertificateValidationCallback = new                  RemoteCertificateValidationCallback(OnValidationCallback);
        }
private static bool OnValidationCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
{
if (cert.subject == MyCertSubject)
       return true;
else
       return false;
}

Now I have to call another webservice using ssl in the code and want to switch to default ssl check before calling that. What's the best way to do that. MS help says the default value of ServicePointManager.SecurityProtocol is null(http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.securityprotocol.aspx). Will setting it to null switch to default ssl validation and is there any other way to do this.

like image 988
Amit Wadhwa Avatar asked Dec 12 '22 20:12

Amit Wadhwa


1 Answers

I have ran in to a similar problem during the development of a diagnostics tool I have created. I have created a windows service that makes requests to a list of urls. For each url I make a request without certificate validation by setting this:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

and then with the same url I also attempt to make a request with certificate validation by resetting the property back to null which should provide the default functionality:

ServicePointManager.ServerCertificateValidationCallback = null;

This has had unexpected results as my request is still made without certificate validation as if the reset has been ignored. After debugging I found that if I waited for long enough after setting

ServicePointManager.ServerCertificateValidationCallback = null;

then it was actually reset and my request was made with default certificate validation. so I simply put a sleep in for 30 seconds before I made the request:

ServicePointManager.ServerCertificateValidationCallback = null;
System.Threading.Thread.Sleep(30000);
//Make request here

I don't understand why this works but it does and I'm sure there is a better way of doing it and if you know how then please let me know.

like image 67
Acode Monkey Avatar answered Feb 27 '23 14:02

Acode Monkey