gpt4 book ai didi

c# - 如何使用GSSAPI认证机制访问MongoDB?

转载 作者:太空宇宙 更新时间:2023-11-03 14:31:37 25 4
gpt4 key购买 nike

我正在尝试使用 c# 通过 ssl 证书连接到 MongoDB 服务器。我收到 System.TimeoutException(使用 CompositeServerSelector 选择服务器 30000 毫秒后发生超时)。

我从通过 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(settings);

var database = client.GetDatabase("DatabaseName");
var collection = database.GetCollection<BsonDocument>("CollectionName");

//This is the place of error
var count1 = collection.CountDocuments(new BsonDocument());

我尝试使用 ConnectTimeout、SocketTimeout 和 wTimeOut,但错误是一样的。

我还尝试使用提到的连接字符串做同样的事情 here但我不知道如何使用这么多参数创建连接字符串。

最佳答案

找到解决方案。

问题在于使用外部服务器对用户进行身份验证。 MongoDB 服务器正在等待来自该外部服务器的许可,但由于身份验证从未成功,MongoDB 总是导致 System.TimeoutException。

这是修复代码。

settings.ReplicaSetName = "replicaName";

SecureString pwd = new NetworkCredential("intranet/userName", "myPassword").securePassword;
var cred = MongoCredential.CreateGssapiCredential("username/intranet.companyname.com", pwd).WithMechanismProperty("SERVICE_NAME", "serviceName").WithMechanismProperty("CANONICALIZE_HOST_NAME", "true");
settings.Credentials = cred;

关于c# - 如何使用GSSAPI认证机制访问MongoDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52318271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com