Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

AuthenticationException: your certificate validation callback returned false

ErrorThe 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.

  • Applies to .NET 8, .NET 9, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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. 3The Errors line prints None, and the request is still rejected.The callback applies a rule of its own that the certificate failsFix

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 subjectAltName

2Fix

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

Did this fix it?

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 -dates

2Fix

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

Did this fix it?

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

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.