AuthenticationException: your certificate validation callback returned false
The remote certificate was rejected by the provided RemoteCertificateValidationCallback.Short answer
This message means a validation callback was installed, yours or one a library added, and it returned false. .NET then reports no reason of its own. Log the sslPolicyErrors value, the certificate names and every chain status inside the callback, then fix what they show: a name mismatch, a chain error, or a rule in the callback itself.
Fix it fast
1Find out which cause you have
Put a logging callback on the handler that fails. It prints what the runtime passes in, and it keeps today's behaviour by returning true only when there were no errors.
var handler = new HttpClientHandler { ServerCertificateCustomValidationCallback = (request, cert, chain, errors) => { Console.WriteLine($"Requested URI: {request.RequestUri}"); Console.WriteLine($"Errors: {errors}"); Console.WriteLine($"Subject: {cert?.Subject} Issuer: {cert?.Issuer}"); if (chain is not null) foreach (var s in chain.ChainStatus) Console.WriteLine($"Chain: {s.Status} {s.StatusInformation}"); return errors == System.Net.Security.SslPolicyErrors.None; } }; var client = new HttpClient(handler);2Match what you see, then jump to its fix
- 1
The Errors line prints RemoteCertificateNameMismatch, and the Subject is a different host name than the URI you called.The certificate does not carry the name you connected toFix - 2
The Errors line prints RemoteCertificateChainErrors, and one or more Chain lines name a status such as UntrustedRoot, PartialChain, NotTimeValid, Revoked or RevocationStatusUnknown.The certificate chain has errorsFix - 3
The Errors line prints None, and the request is still rejected.The callback applies a rule of its own that the certificate failsFix
- 1
What does the error look like?
The remote certificate was rejected by the provided RemoteCertificateValidationCallback.System.Security.Authentication.AuthenticationException
Cause 1 of 3
The certificate does not carry the name you connected to
1Check
That flag stands for a certificate name mismatch: the name in the URL must match a name on the server certificate. Calling a server by its IP address or by an internal alias that is not in the certificate causes it. List the names the certificate holds.
echo | openssl s_client -connect api.example.com:443 -servername api.example.com 2>/dev/null | openssl x509 -noout -ext subjectAltName2Fix
Call the server by a name that appears in the certificate, or have the certificate reissued with the name you use in its subject alternative names.
// call the name the certificate was issued for
var response = await client.GetAsync("https://api.example.com/health");3Verify
The Errors line prints None and the request returns a status code instead of an exception.
Console.WriteLine(response.StatusCode);Confirmed by learn.microsoft.com
Cause 2 of 3
The certificate chain has errors
1Check
That flag means the chain status array is not empty. Each entry has a flag, and the flags are specific: an untrusted root, a chain that could not be built up to the root, an expired time value, a revoked certificate, or a revocation status that could not be determined because the revocation list is offline or unavailable.
echo | openssl s_client -connect api.example.com:443 -servername api.example.com 2>/dev/null | openssl x509 -noout -issuer -dates2Fix
Act on the flag that your log printed. A private root must be trusted on this machine, an expired certificate must be renewed on the server, and a revocation status that cannot be reached needs the machine to be able to reach the revocation list. Do not answer a chain error by returning true for everything.
// keep the strict rule: accept only when nothing is wrong
return errors == System.Net.Security.SslPolicyErrors.None;3Verify
No Chain lines are printed and the Errors line prints None.
Console.WriteLine(errors);Confirmed by learn.microsoft.com
Cause 3 of 3
The callback applies a rule of its own that the certificate fails
1Check
The boolean your method returns decides whether authentication may succeed, and nothing else is consulted. With a clean Errors value, the rejection comes from your own condition, for example a pinned thumbprint that no longer matches after the certificate was renewed, or a callback that insists on a chain that is not supplied.
grep -rn "ServerCertificateCustomValidationCallback\|RemoteCertificateValidationCallback" --include=*.cs .2Fix
Read the callback's conditions and log the value it compares. Update a pinned value to the renewed certificate, or remove the extra rule and rely on the default checks.
Console.WriteLine($"Thumbprint: {cert?.Thumbprint}");
return errors == System.Net.Security.SslPolicyErrors.None;3Verify
The request returns a status code, and the logged thumbprint matches the one your configuration expects.
Console.WriteLine(response.StatusCode);Confirmed by learn.microsoft.com
How do I stop it coming back?
- Never write a callback that returns true for every certificate: it removes the check and hides this very error.
- Keep one callback per client, with a log line for any value it rejects.
- Alert on certificate expiry and on changes to pinned values before the date arrives.
- Call servers by their DNS names so the certificate name always matches the URL.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
AuthenticationException: the remote certificate is invalid in .NET
System.Security.Authentication.AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot, NotValidTime
The error lists the chain status flags .NET found.
3 causes, each with a fixSecurity
ASP.NET Core cannot create the JWT because the signing key is too short or unusable
IDX10630: The '<key type>' for signing cannot be smaller than '<bits>' bits. KeySize: '<bits>'.
The error happens while the API signs the token, not while it checks one.
3 causes, each with a fixSecurity
.NET cannot convert the string to a JwtSecurityToken
SecurityTokenMalformedException
The handler only accepts a compact JWT: three dot-separated base64url segments for a signed token.
3 causes, each with a fixSecurity
SSH to an EC2 instance fails with Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com: Permission denied (publickey).
On EC2 this error almost always means the instance did not accept your key for that user.
3 causes, each with a fixSecurity
gcloud init cannot finish the sign-in step
Enter authorization code
gcloud init signs in through a browser.
3 causes, each with a fixSecurity
A GitHub Actions SSH deploy step cannot log in to the server
ssh: handshake failed: ssh: unable to authenticate
The runner reached your server but the server refused the login.
4 causes, each with a fix