Free Information Technology Magazines and eBooks

Monday, July 03, 2006

VB.NET: How to accept web service SSL Certificate Policy in run-time?

The Problem. I am creating a .NET windows application that will connect to particular web service located in a HTTPS site. The challenge was, the HTTPS site I'm accessing requires all request to accept it's certificate policy. So the design of my application should accept security certificate dialog boxes automatically. A sample security dialog is shown below:


security policy dialog

The solution. First we have to create a class that will override CheckValidationResult function of System.Security.Cryptography.X509Certificates; Here's the custom code of the class I created:



Imports System.Net
Imports System.Security.Cryptography.X509Certificates

Public Class MyPolicy
Implements ICertificatePolicy

Public Function CheckValidationResult(ByVal srvPoint As ServicePoint, _
ByVal cert As X509Certificate, ByVal request As WebRequest, _
ByVal certificateProblem As Integer) _
As Boolean Implements ICertificatePolicy.CheckValidationResult
'Return True to force the certificate to be accepted.
Return True
End Function
End Class


Then you can call the class anywhere before you execute the web service.


System.Net.ServicePointManager.CertificatePolicy =
New MyPolicy
'Call web service here

Summary. On this solution we override the CheckValidationResult function of System.Net.ServicePointManager.CertificatePolicy class to create a custom one that will accept all security certificate automatically. You can also implement this solution to any .NET languages like C# and C++

Now were done. Happy Coding to all!

11 comments:

Destroyer said...

Just what I need! Five (5) star for you!

Anonymous said...

Nice tip. Just wondering if you have VB .NET code to connect to web service that requires Proxy authentication. Please email it to me @ fklumbao@gmail.com

Anonymous said...

Hallo thanks for this Code.
Its Great for my VBNET Code through an Proxy and helps.

Thomas

Anonymous said...

thats a nice tip.. this is exactly what I was looking for.
thanks.

Nitin

Anonymous said...

Awesome man! You saved me a ton of time.

Marcel Rossi said...

Thank You!
That's a realy great small Tip!

Salut
Marcel

Yass said...

Thanks a Lot... You saved my lot time...
Yassar

Anonymous said...

I am trying to call a Secured Web Service which requires Soap/HTTP header authentication. Can you please send me the VB.Net code to kcsr116@yahoo.com

Thanks!

Anonymous said...

This was what I needed. Searched a lot for it. Thanx

Anonymous said...

Thank you very much! Just what I need.

KEVZ TAPERLA said...

Great post dude, Thanks, works fine.
http://tweakevz.blogspot.com