c# - MongoDB C# 驱动程序 'Remote certificate is invalid'

标签 c# mongodb

我在使用 C# 驱动程序连接到 MongoDB 时遇到问题。该代码可在其他系统上运行,但出于某种原因不想在我的系统上运行。我使用的是最新的 Mongo .net 驱动程序和 core/bson,并且具有正确的连接设置。

我的完整错误是:

A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode = Primary, TagSets = [] } }, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Sharded", Type : "Sharded", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/lon-mongos1.objectrocket.com:33138" }", EndPoint: "Unspecified/lon-mongos1.objectrocket.com:33138", 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.
 at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
 at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
 at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
 at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
 --- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
 at MongoDB.Driver.Core.Connections.SslStreamFactory.<CreateStreamAsync>d__4.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
 at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__47.MoveNext()
 --- End of inner exception stack trace ---
 at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__47.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
 at MongoDB.Driver.Core.Servers.ClusterableServer.<HeartbeatAsync>d__42.MoveNext()" }] }.

我查看了本地的证书,没有发现任何表明它在我这边无效的信息。

来自非 mongo 背景,这不是我太熟悉的东西,但想知道是否有人可以阐明我在这里可能做错了什么?

我在 VS2015 上运行 W7 SP1。

编辑:无论如何,我都尝试过验证证书的方法,但事实证明这也没有任何区别。

我经常运行 fiddler,但我已经删除了 fiddler 的证书,目前我没有运行它。

最佳答案

在大多数情况下,当 MongoDb 使用自签名 SSL 证书并且无法通过本地安装的根证书的标准验证时,会出现此错误。您可以通过在连接字符串中指定以下查询参数来简单地禁用此验证:

sslVerifyCertificate=false

如果您使用 C# 代码配置 MongoDb 驱动程序:

var client = new MongoClient();
client.Settings.VerifySslCertificate = false;

关于c# - MongoDB C# 驱动程序 'Remote certificate is invalid',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39119973/

相关文章:

c# - 运行C#几分钟后计时器错误

mongodb - 微服务应用程序......数据库的 Docker 卷或没有 Docker 卷?

java - MONGODB - 使用 JAVA 查找 "_id"

postgresql - MongoDB 相当于 PostgreSQL 中的 LOWER()

c# - 需要帮助选择数据库服务器

c# - MemoryCache UpdateCallback 不工作

c# - 合并列表并从两者中选择属性

javascript - Mongoose 变量排序

node.js - Put 方法不在后端使用 NodeJS 更新 Angular7 中的 MongoDB

c# - 将 OpenXML 文档嵌入到另一个 OpenXml 文档中