如何使用GSSAPI身份验证机制访问mongodb? [英] How to access mongodb using GSSAPI authentication mechanism?
问题描述
我正在尝试使用c#通过ssl证书连接到MongoDB服务器。我收到一个System.TimeoutException(30000ms后使用CompositeServerSelector选择服务器时发生超时)。
我尝试了什么:
我开始通过MongoClientSetting对象进行连接。以下是代码:
MongoClientSettings settings = new MongoClientSettings();
settings.MaxConnectionLifeTime = new TimeSpan(12,0,0);
settings.UseSsl = true;
settings.VerifySslCertificate = false;
var cert = new X509Certificate2(mongoDBCAFile.cer);
settings.SslSettings = new SslSettings {
ClientCertificates = new [] {cert}
};
settings.Servers = new [] {
new MongoServerAddress(xyz1.intranet.companyname.com,12345),
new MongoServerAddress(xyz2.intranet.companyname。 com,12345)
};
settings.ReplicaSetName =replicaName;
var cred = MongoCredential.CreateGssapiCredential(username@intranet.companyname.com)。WithMechanismProperty(SERVICE_NAME,servicename);
settings.Credential = cred;
var client = new MongoClient(设置);
var database = client.GetDatabase(DatabaseName);
var collection = database.GetCollection< BsonDocument>(CollectionName);
//这是错误的地方
var count1 = collection.CountDocuments(new BsonDocument());
我尝试使用ConnectTimeout,SocketTimeout和wTimeOut,但错误是相同的。
我也尝试使用连接strinG做同样的事情但我不知道如何使用这些参数创建一个连接字符串。
我之前没有尝试过使用mongoDB,但通常超时建议由于配置错误,连接被拒绝。尝试设置connectionString,如下所示:
var connectionString = @ mongodb://username%40REALM.com:password @ myserver /?authMechanism = GSSAPI;
var client = new MongoClient(connectionString);
I am trying to connect to the MongoDB server through a ssl certificate using c#. I am getting a System.TimeoutException (A timeout occurred after 30000ms selecting a server using the CompositeServerSelector).
What I have tried:
I started with connection via MongoClientSetting object. Here is the code:
MongoClientSettings settings = new MongoClientSettings(); settings.MaxConnectionLifeTime = new TimeSpan(12, 0, 0); settings.UseSsl = true; settings.VerifySslCertificate = false; var cert = new X509Certificate2("mongoDBCAFile.cer"); settings.SslSettings = new SslSettings{ ClientCertificates = new[] { cert } }; settings.Servers = new[]{ new MongoServerAddress("xyz1.intranet.companyname.com", 12345), new MongoServerAddress("xyz2.intranet.companyname.com", 12345) }; settings.ReplicaSetName = "replicaName"; var cred = MongoCredential.CreateGssapiCredential("username@intranet.companyname.com").WithMechanismProperty("SERVICE_NAME", "servicename"); settings.Credential = cred; var client = new MongoClient(settings); var database = client.GetDatabase("DatabaseName"); var collection = database.GetCollection<BsonDocument>("CollectionName"); //This is the place of error var count1 = collection.CountDocuments(new BsonDocument());
I tried playing around with ConnectTimeout, SocketTimeout, and wTimeOut but the error was same.
I also tried doing the same thing using the connection strinG but I wouldn't figure out how to create a connection string with these many parameters.
I haven't tried using mongoDB before, but typically a timeout suggest that a connection was being refused due incorrect configuration. Try to set the connectionString instead like this:
var connectionString = @"mongodb://username%40REALM.com:password@myserver/?authMechanism=GSSAPI"; var client = new MongoClient(connectionString);
这篇关于如何使用GSSAPI身份验证机制访问mongodb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!