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

SCRAM

Salted 挑战响应身份验证机制 (SCRAM) 是一系列身份验证机制,它们使用质询-响应机制对用户进行身份验证。SCRAM-SHA-256(使用 SHA-256算法对密码进行哈希处理)是MongoDB Server 4.0 及更高版本中的默认身份验证机制。 SCRAM-SHA-1 改用 SHA-1算法,是早于 4.0 的MongoDB Server版本中的默认身份验证机制。

您可以使用SCRAM对MongoDB Atlas、 MongoDB Enterprise Advanced和MongoDB Community Edition进行身份验证。

提示

SCRAM 机制

To learn more about the SCRAM family of authentication mechanisms, see RFC 5802 and Salted Challenge Response Authentication Mechanism on Wikipedia.

For more information about the MongoDB implementation of SCRAM, see SCRAM in the MongoDB Server manual.

本页上的代码示例使用以下占位符:

  • +srv: Include this option in your connection string prefix only if you are connecting to a MongoDB Atlas cluster. To learn more about the +srv option, see Connection String Formats in the MongoDB Server manual.

  • <db_username>:要进行身份验证的用户的MongoDB用户名。

  • <db_password>:要进行身份验证的用户的MongoDB密码。

  • <hostname>: MongoDB 部署的网络解决。

  • <port>: MongoDB 部署的端口号。如果省略此参数,驱动程序将使用默认端口号 (27017)。连接MongoDB Atlas 集群时无需端口号。

  • <authenticationDb>:包含用户身份验证数据的 MongoDB 数据库。如果省略此参数,驱动程序将使用默认值 admin

要使用本页上的代码示例,请将这些占位符替换为您自己的值。

要使用SCRAM对MongoDB用户进行身份验证,请指定您的MongoDB凭证,但不要指定身份验证机制。 您可以在连接字符串中或使用 MongoCredential对象来指定MongoDB凭证。 选择 Connection StringMongoCredential标签页,查看相应的语法:

var mongoClient = new MongoClient(
"mongodb[+srv]://<db_username>:<db_password>@<hostname>[:<port>]/?" +
"authSource=<authenticationDb>");
var credential = MongoCredential
.CreateCredential("<authenticationDb>", "<db_username>", "<db_password>");
var settings = MongoClientSettings.FromConnectionString("<connection string>");
settings.Credential = credential;
var mongoClient = new MongoClient(settings);

要学习;了解有关本页讨论的任何方法或类型的更多信息,请参阅以下API文档: