Jarloo

Menu

WebClient with Certificate Support

By default the WebClient does not support using a certificate, for this you normally need to use a HttpWebRequest, but you can create your own subclass of the
WebClient that lets you supply a certificate when using a WebClient.

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

namespace Jarloo
{
    public class CertificateWebClient : WebClient
    {
        private readonly X509Certificate2 certificate;

        public CertificateWebClient(X509Certificate2 cert)
        {
            certificate = cert;
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
            request.ClientCertificates.Add(certificate);
            return request;
        }
    }
}

Categories:   Code

Comments