65.9K
CodeProject is changing. Read more.
Home

Bypass and Restore SSL Certificate Validation in VB.NET

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.75/5 (4 votes)

Jan 30, 2012

CPOL
viewsIcon

88845

How to bypass SSL certificate validation checking and restore it

Introduction

Sometimes when developing web applications, we get an exception when calling web service/another web application with HTTPS on the URL:

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

The best solution is by installing the SSL certificate on the web server, where you can find here. However sometimes your network engineers told you that they have installed it, but it is still throwing an exception.

A workaround to this issue is to bypass the SSL certificate validation. It is not the best solution, but it works, especially when your network people cannot resolve this issue and you have no authority to touch the production server.

Bypass SSL Certificate Validation

To bypass the SSL certificate validation, we will need to add some code like below:

'ByPass SSL Certificate Validation Checking
System.Net.ServicePointManager.ServerCertificateValidationCallback = _
  Function(se As Object, _
  cert As System.Security.Cryptography.X509Certificates.X509Certificate, _
  chain As System.Security.Cryptography.X509Certificates.X509Chain, _
  sslerror As System.Net.Security.SslPolicyErrors) True

'Call web application/web service with HTTPS URL

'Restore SSL Certificate Validation Checking
System.Net.ServicePointManager.ServerCertificateValidationCallback = Nothing

The first block will bypass any SSL certificate validation checking, while the last block is to restore the SSL certificate validation checking.