-
Notifications
You must be signed in to change notification settings - Fork 0
/
x509.cs
51 lines (41 loc) · 1.74 KB
/
x509.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Collections.Generic;
using System.IO;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
// Tested using MongoDB.Driver 2.9.2 and netcoreapp 2.2
namespace WorkingWithMongoDB
{
class Program
{
static void Main(string[] args)
{
MainAsync().Wait();
}
static async Task MainAsync()
{
var connectionString = "mongodb+srv://balboaclusterm10-5sfvt.mongodb.net/test?retryWrites=true&w=majority&authMechanism=MONGODB-X509";
var settings = MongoClientSettings.FromConnectionString(connectionString);
// You will need to convert your Atlas-provided PEM containing the cert/private keys into a PFX
// use openssl and the following line to create a PFX from your PEM:
// openssl pkcs12 -export -in <x509>.pem -inkey <x509>.pem -out <x509>.pfx -certfile <x509>.pem
// and provide a password, which should match the second argument you pass to X509Certificate2
var cert = new X509Certificate2("x509-temp.pfx", "password");
settings.SslSettings = new SslSettings
{
ClientCertificates = new List<X509Certificate>()
{
cert
}
};
var client = new MongoClient(settings);
// just doing a quick read to verify the usability of this connection
var database = client.GetDatabase("testDB");
var collection = database.GetCollection<BsonDocument>("testCol");
var docCount = collection.CountDocuments("{}");
Console.WriteLine(docCount);
}
}
}