Monday, September 12, 2011

Common Problems while Consuming Web Services over SSL in .NET-Resolved


To consume https enabled web service on test environment, a self-signed certificate (test certificate) is being used.  
Check this link on this blog to install test certificate on your web server (IIS).

When we try to access an https/SSL enabled web service from our C# code, the following error comes.

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

This unhandled exception basically generated by the 'System.Net.WebException' as because of that we do not any pop dialog box in code asking to trust the certificate like following.



To resolve the above said problem, we have to write the following code above the method call of web service to Trust the certificate programmatically.

using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

ServicePointManager.ServerCertificateValidationCallback =
delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return (true);
            };
Here your method call goes on


Happy programming 

1 comment: