Skip to content

Instantly share code, notes, and snippets.

@lothrop
Created March 9, 2018 05:59
Show Gist options
  • Save lothrop/23a745be2a99ed56fddc5b1cfb7ad7f5 to your computer and use it in GitHub Desktop.
Save lothrop/23a745be2a99ed56fddc5b1cfb7ad7f5 to your computer and use it in GitHub Desktop.
Code describing certificate pinning for .NET.
// Put this line in initialization code
ServicePointManager.ServerCertificateValidationCallback = CheckCertificate;
private static bool CheckCertificate(
object sender, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslpolicyerrors)
{
// public key for www.microsoft.com
const string rxpectedPublicKey = "3082010A0282010100CCEAE2843C1BA9352E015D159D854E91CDAC153F6EE5168E1E8803A5A041DA5D83350E83D4271C6DFAECA1C2493CC8864528B2BD00A5F4AADA935453A1DD3164EFBB8624A95FCAE82956CFB9B0619F7E1774CB67064B23A5B492DC7FFBF7D6D46378DFF1362F42787B5C2B8EA4B2A829F647530DDD48BB10CEF5F378E4B44F66446E3A9372C9700794CC950CEE177E0B7C0981FFB2C9ABD59A98AFDF1D3BD880894F9E16BCFA86E0420097C5CCC5D6CE76E9C2BB1DE354E3139CCF2641DA07D04E2AE2D9C927C44212117B07B116D257953F3C2A3E927C8A1EDA7699C6A0D6FED415573471203D3DDA65DD5448CBD8C67A1A870D935A4F7B3A98F303948E003B0203010001";
var publicKey = certificate?.GetPublicKeyString();
return publicKey == expectedPublicKey;
}
var client = new HttpClient();
var result = await client.GetStringAsync("https://www.microsoft.com/");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
@Alialhosh
Copy link

x

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