title: MongoDB C# SSL Client Certificate
body: <p>I am trying to establish a secure connection to MongoDB with the C# driver using certificate validation, but I am getting this error:</p>
  
  <blockquote>
    <p>Unable to connect to server localhost:27017: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine..</p>
  </blockquote>
  
  <p>Heres the error from MongoDB:</p>
  
  <pre><code>[initandlisten] connection accepted from 127.0.0.1:26163 #2 (1 connection now open)
  [conn2] ERROR: no SSL certificate provided by peer; connection rejected
  [conn2] SocketException handling request, closing client connection: 9001 socket exception [CONNECT_ERROR]
  </code></pre>
  
  <p>When I connect to MongoDB through the mongo shell with the certificate it works.</p>
  
  <pre><code>var connectionString = "mongodb://localhost";
  var clientSettings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
  clientSettings.SslSettings = new SslSettings();
  clientSettings.UseSsl = true;
  clientSettings.SslSettings.ClientCertificates = new List&lt;X509Certificate&gt;()
      {
          new X509Certificate("cert.pem")
      };
  clientSettings.SslSettings.EnabledSslProtocols = SslProtocols.Default;
  clientSettings.SslSettings.ClientCertificateSelectionCallback =
      (sender, host, certificates, certificate, issuers) =&gt; clientSettings.SslSettings.ClientCertificates.ToList()[0];
  clientSettings.SslSettings.ServerCertificateValidationCallback = (sender, certificate, chain, errors) =&gt; true;
  var client = new MongoClient(clientSettings);
  </code></pre>
  
  <p>Does anyone know how to get this working?</p>