Created
February 1, 2010 17:13
-
-
Save chilversc/291827 to your computer and use it in GitHub Desktop.
Verify SSL thumbprint
This file contains 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.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