对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

为连接启用 TLS

In this guide, you can learn how to connect to MongoDB instances with the TLS/SSL security protocol using the underlying TLS/SSL support in the .NET Framework. To configure your connection to use TLS/SSL, enable the TLS/SSL settings in either the connection string or MongoClientSettings.

重要

TLS 1.2

.NET/ C#驱动程序仅支持 TLS 1.2 或更高版本。

默认情况下,连接到 MongoDB 实例时禁用 TLS。您可以通过两种不同的方式为 MongoDB 实例的连接启用 TLS:使用 MongoClientSettings 对象上的属性或通过连接字符串中的参数。

注意

如果您使用 DNS 种子列表协议进行连接,则驱动程序默认启用 TLS/SSL。要将其禁用,请在连接字符串或 MongoClientSettings 实例中将 tlsssl 参数值设为 false

要进一步了解使用 DNS 种子列表时的连接行为,请参阅服务器手册中的 SRV 连接格式部分。

要使用 MongoClientSettings 对象启用 TLS,请将 UseTls 属性设置为 true

var settings = new MongoClientSettings { UseTls = true };
var client = new MongoClient(settings);

要使用连接string 为参数tls 分配值true stringMongoClient

var mongoClient = new MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>?tls=true");

您可以使用 MongoClientSettings 配置 X.509 证书。以下代码示例使用名为 client.p12 的证书文件创建了新的 X.509 证书对象,该证书文件受密码 mySuperSecretPassword 保护。然后代码会将该证书添加到 MongoClientSettings 中的 SslSettings.ClientCertificates 数组。

var cert = new X509Certificate2("client.p12", "mySuperSecretPassword");
var settings = new MongoClientSettings
{
SslSettings = new SslSettings
{
ClientCertificates = new[] { cert }
},
UseTls = true
};

重要

加载带有密码的证书时,证书对象必须包含私钥。否则,证书不会传递到服务器。

启用 TLS 后,.NET/C# 驱动程序会自动验证服务器提供的证书。测试代码时,可以禁用证书验证。这称为不安全的 TLS

使用不安全的 TLS 时,唯一的要求是服务器提供 X.509 证书。即使满足以下任一条件,驱动程序也将接受证书:

  • 服务器的主机名与证书上的主题名称(或主题备用名称)不匹配。

  • 证书过期或无效。

  • 证书链中没有受信任的根证书。

  • 证书用途对服务器标识无效。

允许不安全 TLS 的方式有两种:使用 MongoClientSettings 对象上的属性或通过连接字符串中的参数。

要允许使用 MongoClientSettings 对象进行不安全的 TLS,请将 AllowInsecureTls 属性设置为 true

var settings = new MongoClientSettings
{
UseTls = true,
AllowInsecureTls = true
};
var client = new MongoClient(settings);

要使用连接string支持不安全的 TLS,请为连接string参数 tlsInsecure 分配值 true

var mongoClient = new MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>?tls=true&tlsInsecure=true");

警告

在生产中始终将此选项设置为 false。出于安全考虑,服务器证书必须经过正确验证。

当 X.509 证书不再受信任时(示例,如果其私钥已泄露),证书颁发机构将撤销该证书。

默认情况下,.NET/C# 驱动程序在连接之前不会检查服务器的证书是否已被吊销。您可以使用 MongoClientSettings 或连接字符串启用吊销检查。

要使用 MongoClientSettings 启用撤销检查,请将 SslSettings.CheckCertificateRevocation 设置为 true

var settings = new MongoClientSettings
{
SslSettings = new SslSettings
{
CheckCertificateRevocation = true
},
UseTls = true
};

要使用连接string启用撤销检查,请为连接string参数 tlsDisableCertificateRevocationCheck 分配值 false

var mongoClient = new MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>?tls=true&tlsDisableCertificateRevocationCheck=false");

注意

The .NET/C# Driver doesn't check revocation by default because this is the default behavior of the SslStream class in both the .NET Framework and the .NET standard.

.NET/C# 驱动程序在 Windows、macOS 和 Linux 上以不同方式支持以下撤销检查机制:

在 Windows 上,.NET/C# 驱动程序在 .NET 框架和 .NET Core 中均支持 OCSP、OCSP 装订以及不使用 OCSP 的 CRL。

警告

在 Windows 上,如果 OCSP 响应程序不可用,.NET/C# 驱动程序将报告“硬故障”并取消 TLS 握手。其他操作系统和驱动程序将报告“软故障”并继续连接。

在 macOS 上,.NET/C# 驱动程序支持 OCSP 和 OCSP 装订。

从 .NET Core 2.0 开始,驱动程序支持没有 OCSP 的 CRL。

在 Linux 平台上,.NET/C# 驱动程序支持 OCSP、OCSP 装订以及不使用 OCSP 的 CRL。

注意

Let's Encrypt OCSP 已弃用

Let's Encrypt has ended OCSP support for newer certificates. New certificates issued by Let's Encrypt might not include an OCSP responder URL. OCSP revocation checking occurs only when a certificate contains an OCSP URI, so a missing OCSP URL is expected and doesn't necessarily indicate a problem with your MongoDB deployment.

要了解有关本指南中讨论的任何连接选项的详情,请参阅以下 API 文档: