Skip to content

Instantly share code, notes, and snippets.

@rberrelleza
Created September 13, 2012 01:53
Show Gist options
  • Save rberrelleza/3711357 to your computer and use it in GitHub Desktop.
Save rberrelleza/3711357 to your computer and use it in GitHub Desktop.
Use client certificate authentication with System.Net.WebClient
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Net.Security;
namespace WebClientClientCertificateAuth
{
class Program
{
static void Main(string[] args)
{
MyClient webclient = new MyClient();
webclient.UploadData("https://myserver/myresource", "PUT", new byte[0]);
}
class MyClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
X509Store store = new X509Store("My", StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindByThumbprint, "CERT_THUMBPRINT", false);
var cert = certificates[0];
(request as HttpWebRequest).ClientCertificates.Add(cert);
return request;
}
}
}
}
@zebpaypramod
Copy link

its not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment