.NET: The remote certificate is invalid according to the validation procedure
This exception is a wrapper, not a root cause. .NET raises it whenever SslStream finishes the TLS handshake but the server certificate fails validation — expired, wrong hostname, incomplete chain, untrusted root, or a broken system clock. The real reason is always in the inner exception or in X509ChainStatus, and the fix depends on whether you are on .NET Framework (Windows/Schannel) or .NET Core/5+ (OpenSSL on Linux, Schannel on Windows, Security.framework on macOS).
Typical stack trace:
System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
at System.Net.Security.SslStream.StartSendAuthResetSignal(...)
at System.Net.Security.SslStream.AuthenticateAsClient(...)
1. What the Validation Procedure Actually Checks
Before blaming the server, know what .NET validates:
- Time validity —
NotBefore/NotAfteragainst the local clock (UTC, so timezone alone is rarely the cause, but a wrong clock is). - Name matching — the requested host must appear in the Subject CN or, preferably, a SAN entry.
- Chain building — leaf → intermediate(s) → a root in the trusted store (
LocalMachineRoot+CurrentUserRooton Windows,/etc/ssl/certson Linux). - Usage — Key Usage and EKU must permit server authentication.
- Revocation — off by default in most .NET paths, but can produce
RevocationStatusUnknownwhen enabled.
On Linux, .NET Core shells out to OpenSSL and uses the distro CA bundle. A container built FROM mcr.microsoft.com/dotnet/runtime has certificates; a scratch or chiseled image may not.
2. First Five Minutes: Reproduce Outside .NET
If the browser works and .NET fails, the difference is usually the trust store or the chain the client can build. Test with independent tools first:
# Linux / macOS / WSL
openssl s_client -connect api.example.com:443 -servername api.example.com -showcerts </dev/null
# Look for: Verify return code: 0 (ok)
curl -vI https://api.example.com
# Windows PowerShell (uses .NET Framework under the hood in 5.1)
Invoke-WebRequest https://api.example.com -UseBasicParsing
# Windows certificate chain verification
certutil -verify -urlfetch C:tempserver.cer
If openssl reports Verify return code: 20 (unable to get local issuer certificate), the server is not sending the intermediate — no client-side trust change will fix that; fix the server.
3. Cause → Symptom → Fix Comparison
| Cause | What you see | Typical environment | Fix |
|---|---|---|---|
| Expired certificate | NotTimeValid; certificate has expired |
Any | Renew/replace the cert; confirm clock first |
| Hostname / SAN mismatch | RemoteCertificateNameMismatch |
Any | Use the name in the SAN, or reissue with the correct SAN |
| Missing intermediate | PartialChain; unable to get local issuer certificate |
Any, common with nginx/IIS | Install the full chain on the server |
| Untrusted / private root | UntrustedRoot; self signed certificate in certificate chain |
Corporate PKI, self-signed | Import the root CA into the client trust store |
| Clock skew | NotTimeValid on every cert in the chain |
VMs, containers, dual-boot | Enable NTP; w32tm /resync or timedatectl set-ntp true |
| No CA bundle in container | unable to get local issuer certificate only inside the image |
Alpine, distroless, scratch | apk add ca-certificates or copy the bundle |
| TLS-inspecting proxy | UntrustedRoot pointing at a corporate CA |
Corporate networks, Zscaler/Netskope | Trust the proxy root, or bypass inspection for that host |
| Old TLS defaults (.NET Framework) | Could not create SSL/TLS secure channel |
.NET Framework 4.x | Enable TLS 1.2 via ServicePointManager or registry |
4. Fixing It on Windows (.NET Framework and .NET Core)
Step 1 — check the clock and timezone. A few minutes of drift is enough for a freshly issued cert.
w32tm /query /status
w32tm /resync /force
tzutil /g
Get-TimeZone
Step 2 — update the root store. Windows ships root updates through Windows Update. Trigger a scan, then re-check the store:
UsoClient StartScan
# or on older builds: wuauclt /updatenow
Get-ChildItem Cert:LocalMachineRoot |
Where-Object { $_.Subject -match "ISRG|DigiCert|Corp Root" } |
Format-List Subject, Thumbprint, NotAfter
Step 3 — import the missing root. For a private or corporate CA, import into the machine store (requires an elevated shell). certmgr.msc only affects the current user; services and IIS app pools usually need the machine store.
Import-Certificate -FilePath C:tempcorp-root.cer `
-CertStoreLocation Cert:LocalMachineRoot
Step 4 — verify the chain. certutil gives you the same ChainStatus flags .NET sees:
certutil -verify -urlfetch C:tempserver.cer
certutil -dump C:tempserver.cer
Step 5 — check for interception. If the issuer is not the public CA you expect, a proxy is terminating TLS. Import the proxy’s root CA into LocalMachineRoot, or ask network security to exclude the endpoint. Also check netsh winhttp show proxy and the HTTP_PROXY/HTTPS_PROXY environment variables.
5. Fixing It on Linux and in Containers (.NET Core / .NET 5+)
On Linux, .NET Core reads the OpenSSL default bundle (/etc/ssl/certs/ca-certificates.crt on Debian/Ubuntu, /etc/pki/tls/certs/ca-bundle.crt on RHEL family). If the bundle is stale or absent, every public HTTPS call fails.
# Debian / Ubuntu
sudo apt-get update && sudo apt-get install -y ca-certificates
sudo update-ca-certificates
# RHEL / Fedora / CentOS
sudo dnf install -y ca-certificates
sudo update-ca-trust extract
# Alpine
apk add --no-cache ca-certificates && update-ca-certificates
For a private root, drop the PEM (with a .crt extension) into the local trust directory and refresh:
sudo cp corp-root.crt /usr/local/share/ca-certificates/corp-root.crt
sudo update-ca-certificates
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt corp-root.crt
You can also point .NET at a custom bundle without touching the system store:
export SSL_CERT_FILE=/opt/certs/ca-bundle.crt
export SSL_CERT_DIR=/opt/certs
In Docker, install the package in the runtime stage — not just the build stage — or copy the bundle into minimal images:
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates
&& rm -rf /var/lib/apt/lists/*
Verify inside the container, not on the host: docker run --rm myimage openssl s_client -connect api.example.com:443 -servername api.example.com </dev/null | grep "Verify return code".
6. Reading the Chain: Diagnostics That Name the Real Problem
Dump the chain and dates from the server:
openssl s_client -connect api.example.com:443 -servername api.example.com -showcerts </dev/null 2>/dev/null
| openssl x509 -noout -subject -issuer -dates -ext subjectAltName
To see exactly what .NET rejects, hook the validation callback and print every status flag. This works for HttpClient, gRPC, and SslStream:
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
static bool Validate(object sender, X509Certificate? cert, X509Chain? chain, SslPolicyErrors errors)
{
Console.WriteLine($"SslPolicyErrors: {errors}");
if (chain is not null)
{
foreach (var element in chain.ChainElements)
Console.WriteLine($" Subject: {element.Certificate.Subject}");
foreach (var status in chain.ChainStatus)
Console.WriteLine($" ChainStatus: {status.Status} - {status.StatusInformation.Trim()}");
}
return errors == SslPolicyErrors.None;
}
var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = Validate
};
using var client = new HttpClient(handler);
await client.GetStringAsync("https://api.example.com");
The callback still returns false on failure, so this is diagnostic only — not a bypass.
7. Troubleshooting: Real Error Messages and What They Mean
System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.— .NET Framework. The inner exception orX509ChainStatusholds the reason.System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.— .NET Core/5+. Unwrap one more level; you will usually find theAuthenticationExceptionor an OpenSSL string.RemoteCertificateNameMismatch— the URL host is not in the SAN. Common when calling an IP, alocalhostalias, or a load balancer name.RemoteCertificateChainErrorswithUntrustedRoot— the root is not in the store. On Windows this is often a missing Windows Update root; on Linux, a missing CA package.PartialChainorunable to get local issuer certificate— the server did not send the intermediate. Fix the server’sssl_certificate/fullchain.pem, not the client.NotTimeValid/certificate has expired— checkNotAfterand the system clock. In containers, the host clock is shared; in VMs, check for suspended/resumed instances.self signed certificate in certificate chain— a private CA or a TLS-inspecting proxy. Import the root or stop inspecting that host.AuthenticationException: The remote certificate was rejected by the provided RemoteCertificateValidationCallback.— your own callback returnedfalse; it is not a server problem.Win32Exception: The client and server cannot communicate, because they do not possess a common algorithm— a protocol/cipher mismatch, not a certificate problem. Enable TLS 1.2+ on .NET Framework.
8. If You Must Bypass Validation (and Why You Usually Shouldn’t)
Disabling validation turns every HTTPS call into plaintext-equivalent trust. Use it only for a pinned test host, never as a production fix.
// .NET Core / 5+ — accepts any certificate. Do not ship this.
var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
};
// .NET Framework
ServicePointManager.ServerCertificateValidationCallback = (s, c, ch, e) => true;
A safer middle ground is to trust one specific root without touching the machine store:
var root = new X509Certificate2("corp-root.cer");
var chain = new X509Chain();
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
chain.ChainPolicy.CustomTrustStore.Add(root);
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
For local ASP.NET Core development, dotnet dev-certs https --trust fixes the development certificate only — it does nothing for a remote API.
9. FAQ
Does this error mean the server is compromised? No. It means the client could not build a trusted chain. The most common causes are an expired cert, a missing intermediate, or a missing root on the client.
Why does Chrome work but my .NET app fails? Browsers use their own root store and fetch missing intermediates via AIA. .NET uses the OS store and does not chase AIA the same way, so a server that omits the intermediate can pass in a browser and fail in .NET.
We did not change anything — why did it break today? Certificates expire on a fixed date, and root cross-signs expire too. Check NotAfter on every element, including the root, and check whether a Windows Update or a base-image rebuild changed the trust store.
How do I fix it in Docker? Install ca-certificates in the runtime stage and run update-ca-certificates. For private CAs, copy the PEM to /usr/local/share/ca-certificates/ before refreshing, or set SSL_CERT_FILE.
Is it safe to set ServerCertificateCustomValidationCallback to always return true? No. It disables hostname and chain checks, so any MITM or DNS hijack succeeds silently. Fix the trust store instead.
Can I trust a self-signed certificate without disabling validation? Yes — add the certificate to LocalMachineRoot on Windows or the system CA directory on Linux, or use X509ChainTrustMode.CustomRootTrust in code. The SAN must still match the hostname.
How do I know which certificate .NET actually sees? Use the diagnostic callback in section 6 and print chain.ChainElements plus chain.ChainStatus. That output names the failing element and the exact status flag.