Allowing Untrusted SSL Certificates with HttpClient
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, HttpClient rejects HTTPS requests to servers with untrusted SSL certificates (self-signed, expired, or wrong hostname). During development and testing, you may need to bypass this validation using HttpClientHandler.ServerCertificateCustomValidationCallback. This should never be used in production — it disables the protection that prevents man-in-the-middle attacks.
The Error
This happens because the server's certificate is not trusted by the system's certificate store.
Bypassing SSL Validation (Development Only)
DangerousAcceptAnyServerCertificateValidator is a built-in delegate that returns true for all certificates. The name is intentionally alarming.
Custom Validation Logic
Instead of accepting everything, you can validate specific properties:
The callback receives four parameters:
message: The HTTP request messagecert: The server's X509 certificatechain: The certificate chainerrors: What SSL policy errors were detected
Using IHttpClientFactory (ASP.NET Core)
In ASP.NET Core, configure named or typed clients through dependency injection:
Environment-Conditional Bypass
Only bypass in development, never in production:
Or use a configuration flag:
The Right Fix: Trust the Certificate
Instead of bypassing validation, add the self-signed certificate to the trusted store:
After trusting the certificate, HttpClient accepts it without any code changes.
.NET Framework (Older Approach)
In .NET Framework (not .NET Core), the global ServicePointManager controls SSL validation:
In .NET Core and .NET 5+, use HttpClientHandler per-client instead.
Common Pitfalls
- Using bypass in production: Disabling SSL validation exposes all traffic to man-in-the-middle attacks. Attackers can intercept, read, and modify every request and response. Always use proper certificates in production.
- Global ServicePointManager: In .NET Framework,
ServicePointManager.ServerCertificateValidationCallbackaffects every HTTP connection in the process, including third-party libraries. Prefer per-handler callbacks. - Singleton HttpClient with handler:
HttpClientshould be long-lived (or useIHttpClientFactory), but the handler's SSL bypass stays active for the client's lifetime. Ensure this is intentional. - Certificate pinning conflict: If you implement certificate pinning (checking a specific thumbprint), updating the server certificate requires updating the pinned hash in your code. Plan for certificate rotation.
- Docker/container environments: Containers often lack the host's trusted certificates. Mount the CA certificate into the container or add it during the Docker build rather than bypassing validation.
Summary
- Use
HttpClientHandler.ServerCertificateCustomValidationCallbackto bypass SSL validation in development DangerousAcceptAnyServerCertificateValidatoraccepts all certificates — use only for testing- Write custom validation to accept specific certificates by thumbprint or domain
- Use
IHttpClientFactoryin ASP.NET Core to configure SSL handling per named client - The proper fix is trusting the certificate in the OS store or using
dotnet dev-certs https --trust - Never bypass SSL validation in production code

