title: RemoteCertificateNameMismatch/RemoteCertificateChainErrors error message - Making a simple API request app
body: <p>I'm learning how API requests work. As part of my learning I am writing a simple app for myself that connects to my company's main app/server through its REST API.</p>
  <p>I have some experience with C# and I sort of understand how TCP handshakes work at a very basic level. I have searched through Google and Microsoft's documentation on how to do this and, after seeing so many ways on how people do it differently, I'm at the end of my patience with it.</p>
  <p>What I want to do:</p>
  <ol>
  <li>Connect to Server</li>
  <li>Send API Key to authenticate (I have set my server to enable REST clients with no client certificate)</li>
  <li>Get the 'Cardholder' JSON information from the server</li>
  <li>Print the information to the Console.</li>
  </ol>
  <p>I'm running the server locally and connect with my 'client' app through port 8904.</p>
  <p>When I run my current script in Visual Studio I get an Exception Unhandled error that says: &quot;<strong>System.Net.Http.HttpRequestException:</strong> 'The SSL connection could not be established, see inner exception... Inner Exception: AuthenticationException: The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch, RemoteCertificateChainErrors</p>
  <p>I've tried Googling how to get around this error, which brought me to adding the 'ServicePointManager.ServerCertificateValidationCallback' lines, but they seem to make no difference to the outcome.</p>
  <p>Here's my code.</p>
  <pre><code>static void Main(string[] args)
  {
      ConnectToAPI();
  }
  
  public static void ConnectToAPI ()
  {
      var client = new HttpClient();
  
      var webRequest = new HttpRequestMessage(HttpMethod.Get, &quot;https://127.0.0.1:8904/api/cardholders&quot;);
  
      webRequest.Headers.Add(HttpRequestHeader.Authorization.ToString(), &quot;XXXX-XXXX-XXXX-MY-API-KEY-GOES-HERE&quot;);
  
      ServicePointManager.ServerCertificateValidationCallback +=
      (sender, certificate, chain, errors) =&gt; {
              return true;
          };
  
      var response = client.Send(webRequest);
  
      using var reader = new StreamReader(webRequest.Content.ReadAsStream());
  
      Console.WriteLine(reader.ReadToEnd());
      Console.ReadLine();
  }
  </code></pre>