title: Why do I encounter RemoteCertificateNameMismatch every time?
body: <p>Consider the following complete program:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net.Security;
  using System.Net.Sockets;
  using System.Security.Cryptography.X509Certificates;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace RemoteCertNameMismatchDiagnosis
  {
  class Program
  {
      private static bool AcceptAllCertificates(object sender, X509Certificate certificate,
                                                                                          X509Chain chain, SslPolicyErrors sslPolicyErrors)
      {
          Console.WriteLine(sslPolicyErrors.ToString());
          return true;
      }
  
      static void Main(string[] args)
      {
          TcpClient client;
          SslStream sslStream;
  
          bool acceptAnyCert = false;
  
          client = new TcpClient("google.com", 443);
          if (acceptAnyCert)
              sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(AcceptAllCertificates), null);
          else
              sslStream = new SslStream(client.GetStream(), false);
  
          try
          {
              sslStream.AuthenticateAsClient("test client");
          }
          catch (Exception e)
          {
              Console.WriteLine(e.ToString());
          }
          Console.ReadLine();
      }
    }
  }
  </code></pre>
  
  <p>It reports this exception</p>
  
  <blockquote>
    <p>System.Security.Authentication.AuthenticationException: The remote
    certificate is invalid according to the validation procedure.</p>
  </blockquote>
  
  <p>every time. By changing <code>acceptAnyCert</code> to true on line 26, I can have it output this</p>
  
  <blockquote>
    <p>RemoteCertificateNameMismatch</p>
  </blockquote>
  
  <p>, leading me to believe it's unhappy with the name on the cert.</p>
  
  <p>This behavior persists whether I point at google.com, amazon.com, or anywhere else on line 28. I don't think google, microsoft, and amazon all have defective certificates. What am I doing wrong?</p>