Created
September 9, 2016 18:43
-
-
Save jasonmimick/1abcf399d688bdc1c8c721061efa946a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using MongoDB.Bson; | |
using MongoDB.Driver; | |
using System.Security.Cryptography.X509Certificates; | |
using System.Security.Authentication; | |
using System.Net.Security; | |
namespace HelloMongoCSharp | |
{ | |
class HelloSSL | |
{ | |
public static void Main (string[] args) | |
{ | |
ConnectSSL(args); | |
} | |
public async static void ConnectSSL(string[] args) { | |
try { | |
var clientSettings = MongoClientSettings.FromUrl(new MongoUrl(args[0])); | |
clientSettings.SslSettings = new SslSettings(); | |
clientSettings.UseSsl = true; | |
clientSettings.SslSettings.EnabledSslProtocols = SslProtocols.Default; | |
clientSettings.SslSettings.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) | |
{ | |
foreach (var item in chain.ChainElements) | |
{ | |
foreach (var elemStatus in item.ChainElementStatus) | |
{ | |
Console.WriteLine( item.Certificate.Subject + "->" + elemStatus.StatusInformation); | |
} | |
} | |
return true; //NOT FOR PRODUCTION: this line will bypass certificate errors. | |
}; | |
//+= validationCallback; | |
Console.WriteLine("Attempting connection to: " + args[0]); | |
var client = new MongoClient(clientSettings); | |
Console.WriteLine(client); | |
var dbs = client.GetServer().GetDatabaseNames(); | |
foreach(var db in dbs) { | |
Console.WriteLine(db); | |
} | |
Console.WriteLine("Press <return> to exit."); | |
Console.ReadLine(); | |
} catch (Exception e) { | |
Console.WriteLine("error--"); | |
Console.WriteLine(e.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment