Overview
PLAIN身份验证机制允许您使用轻量级目录访问协议 (LDAP)用户名和密码对MongoDB进行身份验证。LDAP身份验证使用RFC-4616中定义的 PLAIN 简单身份验证和安全层 (SASL)。
只有在向MongoDB Atlas或MongoDB Enterprise Advanced进行身份验证时,才能使用此机制。
代码占位符
本页上的代码示例使用以下占位符:
<ldap_username>:您的LDAP用户名。<ldap_password>:您的LDAP密码。<cluster_url>: MongoDB 部署的网络解决。
要使用代码示例,请将这些占位符替换为您自己的值。
LDAP (Plain)
PLAIN身份验证机制使用您的用户名和密码向LDAP服务器进行身份验证。
您可以通过将authMechanism 参数设置为PLAIN 并在LDAP 连接string 中包含 用户名和密码来指定此身份验证机制,如以下示例代码所示。
const { MongoClient } = require("mongodb");
// specify the placeholder values for your environment in the following linesconst clusterUrl = "<cluster_url>";const ldapUsername = "<ldap_username>";const ldapPassword = "<ldap_password>";const authMechanism = "PLAIN";
// Connection URIconst uri = `mongodb+srv://${ldapUsername}:${ldapPassword}@${clusterUrl}/?authMechanism=${authMechanism}`;
const client = new MongoClient(uri);
// Function to connect to the serverasync function run() { try { // Establish and verify connection await client.db("admin").command({ ping: 1 }); console.log("Connected successfully to server"); } finally { // Ensures that the client will close when you finish/error await client.close(); }}run().catch(console.dir);API 文档
要学习;了解有关本页讨论的任何方法或类型的更多信息,请参阅以下API文档: