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