Windows .NET in C# Major – Web Interaction With Self Signed SSL Certs. Part 1

I am going to shift gears a bit, actually change cars entirely, and do a windows programming tutorial in Microsoft’s .NET environment using C#. Why? Because the world runs windows and in the World of Windows, the fastest method of development is through the .NET framework and the best language on that framework is, without a doubt, C#. I know. I took a poll. Stats never lie.

In this tutorial we are going to create a class to handle website interactions, giving us a simple way to post or get information from a web server, with or without an SSL cert and even a self signed one. Usually self signed certs present a problem since the cryptographic side tries to validate the authenticity of the cert, presenting problems. We will get around that.

As always, we start out with the imports:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Collections.Specialized;
using System.Collections;
using System.Threading;

Pretty normal stuff except System.Security.Cryptography.X509Certificates, which is, as you guessed, the library we need to do the cert stuff!

namespace TheNamespace
    public class WebInteraction

This stuff is pretty obvious…

        private const string CLIENTAPP = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6";
        private static WebClient wcClient;
        private static Hashtable sCookie;
        private static string sReferer;

The CLIENTAPP const string is used to tell the server what client application we are using. You can make it anything you want, I am faking firefox on windows. The webclient is what we will use to make the connections. We will use a hashtable to keep track of the cookie elements that need to be passed in. And last a string for the referrer should that be needed.

        public static WebClient Client
            get { return wcClient; }
        public static void SetReferer(string referer)
            sReferer = referer;
        public static string Cookies
                return SerializeCookies(); 

Here we have an accessor for the WebClient and referer and then a property to serialize and deserialize the cookie hashtable into a string, which is what it needs to be to be passed to the browser.

        static WebInteraction()
            System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CertCallback);
            wcClient = new WebClient();
            sCookie = new Hashtable();
            sReferer = "";
        static bool CertCallback(object obj, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors errors)
            return true;

Here we have the constructor where we define the Server Certificate Validation Callback, that is to say, the function the program will run in order to validate the certificate it receives from the server. Then we define that method and have it always return true! So there we get around errors on self signed certs.

Next week we’ll look at the interactive methods and parsing methods!

2 thoughts on “Windows .NET in C# Major – Web Interaction With Self Signed SSL Certs. Part 1
  1. Pingback: High Speed Web Blog » Windows .NET in C# Major - Web Interaction With Self Signed SSL Certs. Part 2

Leave a Reply