Skip to content

Instantly share code, notes, and snippets.

@sachinsu
Created May 24, 2018 04:21
Show Gist options
  • Save sachinsu/3efcc7f6eb0cc215d5edbc20fc2e48a1 to your computer and use it in GitHub Desktop.
Save sachinsu/3efcc7f6eb0cc215d5edbc20fc2e48a1 to your computer and use it in GitHub Desktop.
Mutual SSL Handshake in C# using HTTPWebRequest and TLS1.2
private static void FromFile()
{
// You have to use certificate.p12 or certificate.pfx that contains private key. These extensions represent PKCS#12 standard. There can also be whole certificate chain included in these files
var certificatefilePath = "CertificateFile.pfx";
var certpwd = "password";
var URL = "Host_URL";
X509Certificate cert = new X509Certificate(certificatefilePath,certpwd);
var webrequest = WebRequest.Create(URL) as HttpWebRequest;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //TLS 1.2
ServicePointManager.ServerCertificateValidationCallback = ((object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true);
webrequest.ClientCertificates.Add(cert);
webrequest.Credentials = CredentialCache.DefaultNetworkCredentials;
webrequest.Method = WebRequestMethods.Http.Get;
webrequest.ContentType = "application/json; charset=utf-8";
var responseStream = webrequest.GetResponse().GetResponseStream();
}
@sachinsu
Copy link
Author

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