Created
September 13, 2012 01:53
-
-
Save rberrelleza/3711357 to your computer and use it in GitHub Desktop.
Use client certificate authentication with System.Net.WebClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
its not working