title: MongoDB ssl via C# driver: The remote certificate is invalid according to the validation procedure
body: <p>I am trying to test authentication using X.509 self-signed certificate from C# client to MongoDB. I have <strong>succeeded</strong> in running <em>mongod</em> <strong>in a console window</strong> using ssl and connecting to it from another console window with <em>mongo</em> command:</p>
  <p><code>mongod --clusterAuthMode x509 --sslMode requireSSL --sslPEMKeyFile mongodb.pem --sslCAFile client.pem</code></p>
  <p><code>mongo --ssl --sslCAFile mongodb.pem --sslPEMKeyFile client.pem</code></p>
  <p>Certificate files were generated by following <a href="http://demarcsek92.blogspot.dk/2014/05/mongodb-ssl-setup.html" rel="nofollow noreferrer">these instructions</a>.</p>
  <p>MongoDB driver requires pfx file that I generated by:</p>
  <p><code>openssl pkcs12 -export -in client.pem -inkey client-cert.key -out client.pfx</code></p>
  <p>Actual code is below:</p>
  <pre><code>    private static void TryConnect()
      {
          var cert = new X509Certificate2(@&quot;C:\Program Files\MongoDB\Server\3.2\bin\client.pfx&quot;, &quot;test&quot;);
  
          var settings = new MongoClientSettings
          {
              Credentials = new[]
              {
                  MongoCredential.CreateMongoX509Credential(&quot;subject= emailAddress=test@test.com,CN=127.0.0.1,OU=Test,O=Test,L=Cph,C=DK&quot;)
              },
              SslSettings = new SslSettings
              {
                  ClientCertificates = new[] { cert },
              },
              UseSsl = true
          };
  
          settings.Server = new MongoServerAddress(&quot;127.0.0.1&quot;);            
  
          MongoClient client = new MongoClient(settings);
          var db = client.GetServer().GetDatabase(&quot;test&quot;);
  
          db.CreateCollection(&quot;test&quot;);           
      }
  </code></pre>
  <p>The last line throws an exception: <em>Unable to connect to server 127.0.0.1:27017: The remote certificate is invalid according to the validation procedure.</em></p>
  <p>Does anyone know how to get this working?</p>