2017-10-04 91 views
-1

當我們將應用程序部署到測試服務器時,我們無法使用MongoDB驅動程序連接到Cosmos數據庫。無法使用MongoDB驅動程序連接到Azure Cosmos數據庫.Net C#

我們所有的開發機器都沒有問題,但我們正在測試以下內容。 我知道這是它在機器上的證書問題,但我們如何解決它?

[{ ServerId: "{ ClusterId : 1, EndPoint : 
"Unspecified/xxxxxxxxx.documents.azure.com:10255" }", 
EndPoint: "Unspecified/xxxxxxxx.documents.azure.com:10255", State: 
"Disconnected", Type: "Unknown", HeartbeatException: 
"MongoDB.Driver.MongoConnectionException: 
An exception occurred while opening a connection to the server. ---> 
System.Security.Authentication.AuthenticationException: 
The remote certificate is invalid according to the validation procedure. 

問題在這裏客戶端證書無效?

我們連接到Azure SQL實例時沒有任何問題。

回答

0

根據我的理解,您可以使用SslSettings.ServerCertificateValidationCallback獲取有關服務器證書驗證的更多詳細信息,並檢查策略錯誤以縮小此問題。你可以構建你的MongoClientSettings如下建立你的MongoClient

MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl("{connection-string-of-your-cosmosdb}")); 
settings.SslSettings = new SslSettings() 
{ 
    EnabledSslProtocols = SslProtocols.Tls12, 
    ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
    { 
     foreach (var element in chain.ChainElements) 
     { 
      // Gets the error status of the current X.509 certificate in a chain. 
      foreach (var status in element.ChainElementStatus) 
      { 
       Console.WriteLine($"certificate subject: {element.Certificate.Subject},ChainStatus: {status.StatusInformation}"); 
      } 
     } 
     return true; //just for dev, it would bypass certificate errors 
    } 
}; 
var mongoClient = new MongoClient(settings); 
相關問題