【问题标题】:MongoDb driver; "The remote certificate is invalid according to the validation procedure"MongoDb 驱动程序; “根据验证程序,远程证书无效”
【发布时间】:2022-07-06 05:50:24
【问题描述】:

尝试通过以下代码访问 AWS 中最近创建的 documentdb 数据库;

 private readonly IMediator _mediator;
    private readonly IMongoClient _client;
    private readonly IMongoDatabase _database;
    private readonly IMongoCollection<GetDocumentsQuery> _collection;

    public GetDocumentsQueryHandler(IMediator mediator)
    {
        _mediator = mediator;
    
        _client = new MongoClient("mongodb://username:password@yemek-docdb.cluster-cjl2jeqxlldb.eu-central-1.docdb.amazonaws.com:27017/?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false");

        _database = _client.GetDatabase("testdb");
        _collection = _database.GetCollection<GetDocumentsQuery>("testcollection");
    }

    public bool Handle(GetDocumentsQuery message)
    {
        try
        {
            _collection.InsertOne(message);
            var asd = _collection.Find(f => f.Name != "asd").ToList();
        }
        catch (System.Exception ex)
        {
        }
        return true;
    }
}

抛出此异常:

ex {"在选择服务器 30000 毫秒后发生超时,使用 CompositeServerSelector{ 选择器 = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 },操作计数服务器选择器}。集群状态的客户端视图是 { ClusterId:“1”,类型:“未知”,状态:“断开连接”, 服务器:[{ ServerId:“{ ClusterId:1,端点: “未指定/mydocdbname.amazonaws.com:27017”}”,端点: “未指定/mydocdbname.amazonaws.com:27017”,ReasonChanged: “心跳”,状态:“断开连接”,服务器版本:, TopologyVersion:,类型:“未知”,HeartbeatException: “MongoDB.Driver.MongoConnectionException:发生异常时 打开与服务器的连接。 ---> System.Security.Authentication.AuthenticationException:远程 根据验证过程,证书无效。\r\n at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)\r\n 在 System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult 结果)\r\n 在 System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)\r\n 在 System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)\r\n--- 上一个堆栈跟踪的结束 引发异常的位置 ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 MongoDB.Driver.Core.Connections.SslStreamFactory.d__4.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 MongoDB.Driver.Core.Connections.BinaryConnection.d__51.MoveNext()\r\n --- 内部异常堆栈跟踪结束 ---\r\n 在 MongoDB.Driver.Core.Connections.BinaryConnection.d__51.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 MongoDB.Driver.Core.Servers.ServerMonitor.d__33.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务)\r\n 在 MongoDB.Driver.Core.Servers.ServerMonitor.d__35.MoveNext()", LastHeartbeatTimestamp: "2022-07-05T18:31:52.4889181Z", LastUpdateTimestamp: "2022-07-05T18:31:52.4889181Z" }] }."} System.Exception {System.TimeoutException}

这里缺少什么?

【问题讨论】:

  • 你应该提供完整的错误信息
  • @dododo 我不知道你为什么需要所有异常细节,提供
  • 因为这是服务器选择失败的主要原因:System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.,您使用的证书有问题
  • @dododo 是的,你能帮忙解决这个问题吗?
  • 只配置正确的证书 :) 这不是关于编程,而是关于环境设置

标签: amazon-web-services mongodb-.net-driver aws-documentdb


【解决方案1】:

您的问题的答案是 .net 驱动程序不支持ssl_ca_certs 连接字符串选项,您应该在操作系统级别配置 CA 证书

【讨论】:

    猜你喜欢
    • 2011-03-28
    • 2016-11-28
    • 2013-08-08
    • 2012-07-20
    • 2015-03-17
    • 2021-05-30
    • 1970-01-01
    相关资源
    最近更新 更多