HttpClient untrusted SSL certificates: DangerousAcceptAnyServerCertificateValidator
Codemia Knowledge Hub ·
To allow untrusted SSL certificates with HttpClient in C#, set a ServerCertificateCustomValidationCallback on HttpClientHandler that returns true; the simplest version uses the built-in HttpClientHandler.DangerousAcceptAnyServerCertificateValidator. This bypasses all certificate validation, which is acceptable in local development but must never reach production. In production, trust the certificate in the operating system's certificate store or use a properly issued certificate.
The SSL error HttpClient throws
When HttpClient connects to a server whose certificate is untrusted (self-signed, expired, wrong hostname, or issued by an unknown CA), it throws:
The TLS handshake fails because the server's certificate chain cannot be validated against the system's trusted root store.
Using DangerousAcceptAnyServerCertificateValidator
The fastest way to suppress the error in development:
The name is intentionally alarming. This delegate returns true for every certificate regardless of errors, hostname mismatches or expiration.
Accepting only specific certificates
A custom callback can accept only certain certificates, a middle ground between full bypass and full validation:
The callback receives four parameters:
| Parameter | Type | Description |
request | HttpRequestMessage | The outgoing HTTP request |
cert | X509Certificate2 | The server's certificate |
chain | X509Chain | The full certificate chain |
errors | SslPolicyErrors | Flags indicating what validation failed |
The relevant SslPolicyErrors values:
| Value | Meaning |
None | Certificate is fully valid |
RemoteCertificateChainErrors | Chain validation failed (self-signed, unknown CA, expired) |
RemoteCertificateNameMismatch | Certificate hostname does not match the request URL |
If you pin a thumbprint, deploying a new certificate requires a code change and redeployment. Plan for rotation by pinning the public key (SPKI) or the issuer instead.
Configuring the bypass with IHttpClientFactory in ASP.NET Core
In ASP.NET Core, do not create HttpClient instances directly. Use IHttpClientFactory to manage handler lifetimes and configure SSL behavior per named or typed client:
Then inject and use the factory:
This keeps the bypass scoped to one named client instead of all HTTP traffic in the application. Note that ConfigurePrimaryHttpMessageHandler runs when a new handler is created, and handlers are pooled for two minutes by default, so changing configuration at runtime does not immediately affect pooled handlers.
Enabling the bypass only in development
Gate the bypass on the hosting environment or a configuration flag:
With Option 2, set BypassSslValidation in appsettings.Development.json but not in appsettings.Production.json.
Shipping the bypass to production is the one thing you must not get wrong. It exposes every request to man-in-the-middle attacks: anyone on the network can intercept, read and modify the traffic. A singleton HttpClient registered with the bypass is especially risky, since a misconfiguration leaves every request unprotected for the whole process lifetime.
The proper fix: trust the certificate
Add the certificate to the trusted store so HttpClient validates it normally:
After that, HttpClient accepts the certificate with no custom callback. Use this approach in staging and production.
For Docker images built from minimal base images, which often lack the host's trusted certificates, copy the CA certificate into the image during the build (COPY ca.crt /usr/local/share/ca-certificates/ followed by RUN update-ca-certificates) instead of bypassing validation in code.
.NET Framework: ServicePointManager
In .NET Framework (not .NET Core/.NET 5+), SSL validation is controlled globally through ServicePointManager:
This is more dangerous than the per-handler approach because it disables validation for every HttpClient, WebClient and HttpWebRequest in the process, including those created by third-party libraries. It does not work in .NET Core; there, the per-handler ServerCertificateCustomValidationCallback is the only option, and it is safer because it is scoped to one client.
Comparing the approaches
| Approach | Scope | .NET Version | Risk Level |
HttpClientHandler callback | Single client | .NET Core+ | Moderate (scoped) |
IHttpClientFactory handler config | Named/typed client | .NET Core+ | Moderate (scoped) |
ServicePointManager callback | Entire process | .NET Framework | High (global) |
| Trust certificate in OS store | System-wide | Any | Low (proper fix) |
dotnet dev-certs https --trust | Dev machine | .NET Core+ | Low (dev only) |
Related reading
- Amazon API Gateway in front of ELB and ECS Cluster
- Amazon API gateway timeout
- Amazon AWS Route 53 Hosted Zone does not work
- Amazon ec2 not working when accessing through public IP
- Am I trying to connect to a TLS-enabled daemon without TLS?
- Amazon Cognito A client attempted to write unauthorized attribute
- Amazon ELB for EC2 instances in private subnet in VPC
- Amazon ELB in VPC
System design interview problems on this topic
- Design a Load Balancer: solution and walkthrough
- Design a Domain Name System: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.