Skip to content

Instantly share code, notes, and snippets.

@Mardaneus86
Last active April 21, 2017 15:14
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 Mardaneus86/0a4cc5a65f2186fef4dbd5e629290fbb to your computer and use it in GitHub Desktop.
Save Mardaneus86/0a4cc5a65f2186fef4dbd5e629290fbb to your computer and use it in GitHub Desktop.
Code examples for the App security: SSL and certificate pinning blogpost
ServicePointManager.ServerCertificateValidationCallback +=
(sender, certificate, chain, sslPolicyErrors) => {
// ...
}
// validkeys should be a list of strings containing the trusted public keys
ServicePointManager.ServerCertificateValidationCallback +=
(sender, certificate, chain, sslPolicyErrors) => {
return validkeys.Contains(certificate?.GetPublicKeyString());
}
ServicePointManager.ServerCertificateValidationCallback +=
(sender, certificate, chain, sslPolicyErrors) => {
if (certificate == null)
{
return false;
}
foreach (var cert in chain.ChainPolicy.ExtraStore)
{
if (cert.Subject == certificate?.Issuer)
{
return validkeys.Contains(cert.GetPublicKeyString());
}
}
return false;
}
ServicePointManager.ServerCertificateValidationCallback +=
(sender, certificate, chain, sslPolicyErrors) => {
return false; // this effectively disables all certificate checks, never use this approach in production code!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment