我已按照https://docs.mongodb.com/manual/core/security-x.509/文档配置了MongoDB服务器,并使用mongo shell进行连接,一切正常。
接下来,我尝试使用C#驱动程序连接相同的服务器,但出现了超时异常。以下是我的代码:
我查看了MongoDB服务器日志,发现以下错误:
如果您有以上问题的解决方案,请回复。
接下来,我尝试使用C#驱动程序连接相同的服务器,但出现了超时异常。以下是我的代码:
var cert = new X509Certificate2(@"C:\Program Files\MongoDB\Server\3.2\ssl\client.pfx", "secretkey");
var sslcrd = MongoCredential.CreateMongoX509Credential("CN=Client1,O=School,ST=Some-State,C=IN");
settings.SslSettings = new SslSettings() ;
settings.UseSsl = true;
settings.SslSettings.ClientCertificates = new List<X509Certificate>()
{
cert
};
settings.SslSettings.EnabledSslProtocols = SslProtocols.Default;
settings.SslSettings.ClientCertificateSelectionCallback =
(sender, host, certificates, certificate, issuers) => settings.SslSettings.ClientCertificates.ToList()[0];
settings.SslSettings.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
settings.SslSettings.CheckCertificateRevocation = false;
settings.VerifySslCertificate = false;
settings.Credentials = new[] { sslcrd };
MongoClient client = new MongoClient(settings);
var db = client.ListDatabases().ToList();
我查看了MongoDB服务器日志,发现以下错误:
我使用的是c# MongoDB.Driver版本2.3.0和MongoDB包版本3.2。2017-04-10T11:18:21.559+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:53901 #64 (1 connection now open) 2017-04-10T11:18:21.559+0530 E NETWORK [conn64] no SSL certificate provided by peer; connection rejected 2017-04-10T11:18:21.560+0530 I NETWORK [conn64] end connection 127.0.0.1:53901 (0 connections now open)
如果您有以上问题的解决方案,请回复。