Advertisement
Intermediate ASP.NET Core, HTTPS, Certificates & Transport Security

Why is it important to validate a server's TLS certificate when making outbound HttpClient calls to external services, and what's the risk of disabling certificate validation?

Short answer

By default, HttpClient validates the remote server's certificate against trusted CAs, protecting against man-in-the-middle attacks where a malicious actor intercepts and impersonates the intended destination -- disabling this validation (via…

Save ASP.NET Core mock interview
Advertisement
AModel answer
By default, HttpClient validates the remote server's certificate against trusted CAs, protecting against man-in-the-middle attacks where a malicious actor intercepts and impersonates the intended destination -- disabling this validation (via ServerCertificateCustomValidationCallback returning true unconditionally) is sometimes done carelessly to work around a certificate problem during development, but doing so in production removes this fundamental protection entirely, making the application vulnerable to traffic interception even over what appears to be an HTTPS connection.
// DANGEROUS -- disables certificate validation entirely, should never reach production
var handler = new HttpClientHandler {
    ServerCertificateCustomValidationCallback = (msg, cert, chain, errors) => true  // accepts ANY certificate
};

// SAFER -- validate against a specific known issue rather than disabling entirely
var saferHandler = new HttpClientHandler {
    ServerCertificateCustomValidationCallback = (msg, cert, chain, errors) => {
        return errors == SslPolicyErrors.None || IsKnownAcceptableException(cert);
    }
};
Real-world example
A code review catches a developer's temporary workaround (unconditionally returning true from ServerCertificateCustomValidationCallback to bypass a local self-signed certificate issue) that had accidentally been left in and merged toward production, which would have silently disabled TLS certificate validation for all outbound calls from that service.
Likely follow-ups
  • What's a safer, narrower workaround for a legitimate development-only self-signed certificate issue?
  • How would you detect if certificate validation has been inadvertently disabled somewhere in a large codebase?
Related topics