Skip to content
ecdsa.com
.NET SslStream / HttpClient.NET

Error message

The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch

Also printed as

  • The remote certificate is invalid according to the validation procedure.

Older .NET versions printed only "The remote certificate is invalid according to the validation procedure." without naming the error; current .NET appends the SslPolicyErrors value. HttpClient wraps it in "The SSL connection could not be established, see inner exception."

.NET SslStream / HttpClient — SslPolicyErrors.RemoteCertificateNameMismatch

What it means

The server's certificate chain is trusted, but the host name .NET connected to is not among the names the certificate was issued for (its Subject Alternative Names). The text "according to the validation procedure" is used for SslPolicyErrors such as name mismatches, as opposed to "errors in the certificate chain" for trust and date problems.

Why it happens

How to fix it

  1. 1.

    Compare the certificate's names with the URL

    Print the SAN list of the served certificate.

    bash
    echo | openssl s_client -connect api.example.com:443 -servername api.example.com 2>/dev/null \
      | openssl x509 -noout -subject -ext subjectAltName
  2. 2.

    Connect with a name from the certificate

    Change the URL and name resolution rather than switching validation off.

    csharp
    // Use a host name from the certificate in the URL (resolve it to the address you need via DNS)
    using var client = new HttpClient();
    var response = await client.GetAsync("https://www.example.com/health");
  3. 3.

    SQL Server: set HostNameInCertificate

    Microsoft.Data.SqlClient 5.0 and later accept HostNameInCertificate so you can connect by address while validating the certificate's real name; TrustServerCertificate=True instead skips all validation and should not be used outside throwaway environments.

    text
    # Microsoft.Data.SqlClient: connect by IP/alias but validate against the certificate's name
    Server=10.0.0.5;Database=app;Encrypt=True;HostNameInCertificate=sql01.corp.example.com;

Related errors

Browse the full signature error database