AWS documentation states that to connect to my DocumentDB Cluster, I need to use a query string that ends like so ?ssl_ca_certs=rds-combined-ca-bundle.pem&replicaS
Here is another way. However I found that by using SSL with the C# Mongo Driver doesn't do connection Pooling and opened a new connection for each call. You can reduce the active connections by including MaxConnectionIdleTime but it's still not ideal if your application creates a lot of connections.
var connectionString = "username:password@cluster_endpoint:27017/?replicaSet=rs0";
var clientSettings = MongoClientSettings.FromUrl(new MongoUrl("mongodb://" + connectionString));
var certificatePath = "ssl\rds-combined-ca-bundle.pem";
var pem = System.IO.File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + certificatePath);
byte[] certBuffer = GetBytesFromPEM(pem, "CERTIFICATE");
clientSettings.UseSsl = true;
clientSettings.SslSettings = new SslSettings()
{
ClientCertificates = new List()
{
new X509Certificate2(certBuffer)
},
EnabledSslProtocols = System.Security.Authentication.SslProtocols.Default,
CheckCertificateRevocation = true
};
clientSettings.VerifySslCertificate = true;
clientSettings.SslSettings.ClientCertificateSelectionCallback = (sender, host, certificates, certificate, issuers) => clientSettings.SslSettings.ClientCertificates.ToList()[0];
clientSettings.SslSettings.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
clientSettings.MaxConnectionIdleTime = new TimeSpan(0, 0, 30);
_client = new MongoClient(clientSettings);
_database = _client.GetDatabase(db.ToString());