How to Connect to SSL site using WebRequest

A

Anonieko

..NET 1.0 /1.1 version:
======================

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

// ...

private void MethodToAccessSSL()
{
// ...
ServicePointManager.CertificatePolicy = new
AcceptAllCertificatePolicy();

// ... WebRequest myRequest = WebRequest.Create(url); ...
}



internal class AcceptAllCertificatePolicy : ICertificatePolicy
{
public AcceptAllCertificatePolicy() { }
public bool CheckValidationResult(ServicePoint sPoint,
X509Certificate cert, WebRequest wRequest,int certProb)
{
return true;
}
}


..NET 2.0 Version
================

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


private void MethodToAccessSSL()
{
// ...
ServicePointManager.ServerCertificateValidationCallback =
new
RemoteCertificateValidationCallback(ValidateServerCertificate);
// ... WebRequest myRequest = WebRequest.Create(url); ...
}


// The following method is invoked by the
RemoteCertificateValidationDelegate.
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;

Console.WriteLine("Certificate error: {0}", sslPolicyErrors);

// Do not allow this client to communicate with unauthenticated
servers.
return false;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top