Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

AuthenticationException: the remote certificate is invalid in .NET

ErrorSystem.Security.Authentication.AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot, NotValidTime

Short answer

The error lists the chain status flags .NET found. UntrustedRoot means the root is not trusted on this machine. NotTimeValid (shown as NotValidTime) means a certificate in the chain is expired or not yet valid. Read the flags first, then trust the right CA, renew the certificate, or fix the clock.

  • 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

    Look at the server certificate the client actually receives: issuer and validity dates.

    echo | openssl s_client -connect api.example.com:443 -servername api.example.com 2>/dev/null | openssl x509 -noout -issuer -dates
  2. 2Match what you see, then jump to its fix

    1. 1UntrustedRoot when calling https://localhost from another .NET process or container.A local HTTPS development certificate is not trustedFix
    2. 2UntrustedRoot only on Linux or in a container; the issuer from the first step is your own CA.A private CA is missing from the Linux trust storeFix
    3. 3NotValidTime appears in the error; the dates from the first step are in the past or future.A certificate in the chain is expired or not yet validFix

What does the error look like?

  • System.Security.Authentication.AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot, NotValidTime

Cause 1 of 3

A local HTTPS development certificate is not trusted

1Check

The dev certificate is created untrusted unless you ask for trust.

dotnet dev-certs https --check --trust

2Fix

Remove old development certificates, create a new one and trust it.

dotnet dev-certs https --clean
dotnet dev-certs https --trust

3Verify

The check now reports a trusted certificate.

dotnet dev-certs https --check --trust

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

A private CA is missing from the Linux trust store

1Check

Chain status UntrustedRoot means the chain is invalid because of an untrusted root certificate.

sudo ls /etc/ssl/certs/ | grep my-ca

2Fix

Add the CA as a .crt PEM file and update the store.

sudo cp my-ca.crt /usr/local/share/ca-certificates
sudo update-ca-certificates

3Verify

The PEM file is listed, and a restart of the app connects.

sudo ls /etc/ssl/certs/ | grep my-ca

Confirmed by ubuntu.com

Did this fix it?

Cause 3 of 3

A certificate in the chain is expired or not yet valid

1Check

NotTimeValid means a time value is invalid, such as an expired certificate. Also compare the client clock.

date -u
echo | openssl s_client -connect api.example.com:443 -servername api.example.com 2>/dev/null | openssl x509 -noout -enddate

2Fix

Renew the expired certificate on the server. If only the client clock is wrong, correct it.

date -u

3Verify

The end date is in the future and the client date is correct; call the endpoint again.

curl -I https://api.example.com

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Log the chain status flags on failure so the exact flag is in your logs.
  • Alert on certificate expiry well before the date.
  • Do not return true from a ServerCertificateCustomValidationCallback to hide this error.

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

dotnet stops with 'the folder host/fxr does not exist' on Ubuntu

A fatal error occurred. The required library libhostfxr.so could not be found.

This error nearly always means .NET packages came from two different package feeds, usually Ubuntu's own and Microsoft's, and each put files in a different place.

3 causes, each with a fix

Servers

dnf cannot install MySQL on Amazon Linux 2023

dnf cannot find a mysql-server package on Amazon Linux 2023

Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.

3 causes, each with a fix

Next step

Want this handled for good?

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