c# - 通过 MongoDB C# 驱动程序使用 SSL 连接到 DocumentDB

标签 c# mongodb amazon-web-services ssl aws-documentdb-mongoapi

我有一个使用 TLS 的 AWS DocumentDB 集群。我想使用 C# MongoDB DriverC# 中的 .net core 应用程序连接到它。

AWS给出的connection-string包括这部分?ssl_ca_certs=rds-combined-ca-bundle.pem,即the certificate chain given by Amazon .我可以使用此证书与 MongoDB shell 正确连接到数据库。

我的问题是 MongoDB C# Driver 不支持 .pem 文件。它似乎只支持 .pfx 文件。我尝试转换 .pem,但它缺少私钥。我可以从 .pem 生成 .pfx 所需的 .cer,但不能生成私钥。即使这样,这个解决方案似乎也是粗略和非官方的。

有没有一种简单的方法可以通过 MongoDB C# 驱动程序使用 SSL 连接到 DocumentDB?如果是,我应该使用什么解决方案?

最佳答案

使用带有 SSL 的简单 .Net 控制台应用程序连接到 Document DB。

->首先,通过将参数 tls 设置为“已启用”,在文档数据库集群上启用 SSL。确保重新启动集群的写入器节点以重新启动整个集群,以便应用参数组更改。默认情况下,当您启动新的 Doc 数据库集群时启用 TLS。

->在您的环境中设置 SSL 证书:

1)从以下链接在您的源 Windows 机器上下载 PKCS#7 SSL 证书:

https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.p7b

2)点击开始菜单,点击运行并输入mmc

3)在 MMC 中,文件->添加/删除管理单元。

4)从管理单元列表中选择证书并单击添加。

5) 受信任的 CA 证书应该在本地计算机存储中,因此选择“计算机帐户”单选按钮,单击下一步,然后选择“本地计算机”。单击下一步,然后单击完成。

6)现在从左侧 Pane (在控制台根目录下,您将看到“证书”选项。单击它。

7) 将出现一个列表,右键单击“受信任的根证书颁发机构”,然后选择所有任务->导入

8)在打开的窗口中,单击下一步,浏览在步骤 1 中下载的证书 (.p7b) 文件(如果找不到,请从文件类型下拉菜单中选择所有文件),然后然后继续点击下一步,最后完成。然后保存配置。

->然后编写如下代码:

---------------------------------------------------

using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace FirstDocDB
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var connectionString = "mongodb://pulkit:password@ClusterID:27017/?ssl=true&sslVerifyCertificate=true&replicaSet=rs0";
            var client = new MongoClient(connectionString);
            var database = client.GetDatabase("test");
            var collection = database.GetCollection("stuff");
            var document = collection.Find(new BsonDocument()).FirstOrDefault();
            Console.WriteLine(document.ToString());
        }
    }
}

---------------------------------------------------

->在构建和运行之后,我成功地获得了名为“stuff”的集合中的文档作为输出: { "_id": ObjectId("5c5a63b10cf861158c1d241c"), "hello": "world"}

因此,按照上述步骤,我可以使用 .Net 的 Mongo 驱动程序成功连接到 Document DB。

关于c# - 通过 MongoDB C# 驱动程序使用 SSL 连接到 DocumentDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54635990/

相关文章:

javascript - 异常的标识符与等待 : Node. js

java - 无法使用 Morphia 连接到 MongoDb

amazon-web-services - Amplify 安装到现有 Vue/Bootstrap 项目时出错 : You may need an appropriate loader to handle this file type

java - 使用 aws athena 解析 CSV

c# - 为什么 (int)(33.46639 * 1000000) 返回 33466389?

c# - 过滤集合 C# 的最快方法

javascript - Node.js 中的错误导出模块 - 关注点分离

amazon-web-services - DynamoDB - 每个帐户的表数量限制

c# - 从 JavaScript 访问 Silverlight 3.0 应用程序

c# - 使用简短的初始化语法来初始化对值列表