Skip to content

Instantly share code, notes, and snippets.

@chilversc
Created February 1, 2010 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilversc/291827 to your computer and use it in GitHub Desktop.
Save chilversc/291827 to your computer and use it in GitHub Desktop.
Verify SSL thumbprint
using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
namespace SslTest
{
internal class Program
{
private static void Main(string[] args)
{
var url = new Uri("https://ssltest.dev/");
var request = WebRequest.Create(url);
request.Method = "GET";
request.ImpersonationLevel = TokenImpersonationLevel.Anonymous;
ServicePointManager.ServerCertificateValidationCallback += ServerCertificateValidationCallback;
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
Console.WriteLine(reader.ReadToEnd());
}
Console.ReadKey();
}
private static bool ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
const string expectedHash = "B5B99C4FEEF719A3A2B5D43EAD8677053A8847E2";
if (errors != SslPolicyErrors.None)
return false;
var hash = certificate.GetCertHashString();
if (!expectedHash.Equals(hash, StringComparison.Ordinal))
return false;
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment