我正在嘗試使用 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("[email protected]").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 errorvar count1 = collection.CountDocuments(new BsonDocument());我嘗試使用 ConnectTimeout、SocketTimeout 和 wTimeOut,但錯誤是一樣的。我也嘗試使用此處提到的連接字符串做同樣的事情,但我不知道如何使用這么多參數創建連接字符串。
1 回答

慕妹3242003
TA貢獻1824條經驗 獲得超6個贊
找到了解決方案。
問題在于使用外部服務器對用戶進行身份驗證。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;
- 1 回答
- 0 關注
- 175 瀏覽
添加回答
舉報
0/150
提交
取消