Overview概述
The PLAIN authentication mechanism allows you to use your Lightweight Directory Access Protocol (LDAP) username and password to authenticate to MongoDB. PLAIN身份验证机制允许您使用轻量级目录访问协议(LDAP)用户名和密码对MongoDB进行身份验证。LDAP authentication uses the PLAIN Simple Authentication and Security Layer (SASL) defined in RFC-4616.LDAP身份验证使用RFC-4616中定义的PLAIN简单身份验证和安全层(SASL)。
You can use this mechanism only when authenticating to MongoDB Atlas or MongoDB Enterprise Advanced.您只能在向MongoDB Atlas或MongoDB企业高级版进行身份验证时使用此机制。
Code Placeholders代码占位符
The code examples on this page use the following placeholders:此页面上的代码示例使用以下占位符:
<ldap_username>: Your LDAP username.:您的LDAP用户名。<ldap_password>: Your LDAP password.:您的LDAP密码。<cluster_url>: The network address of your MongoDB deployment.:MongoDB部署的网络地址。
To use the code examples, replace these placeholders with your own values.要使用代码示例,请将这些占位符替换为您自己的值。
LDAP (PLAIN)
The PLAIN authentication mechanism uses your username and password to authenticate to an LDAP server.PLAIN身份验证机制使用您的用户名和密码对LDAP服务器进行身份验证。
You can specify this authentication mechanism by setting the 您可以通过将authMechanism parameter to PLAIN and including your LDAP username and password in the connection string as shown in the following sample code.authMechanism参数设置为PLAIN并在连接字符串中包含LDAP用户名和密码来指定此身份验证机制,如以下示例代码所示。
const { MongoClient } = require("mongodb");
// specify the placeholder values for your environment in the following lines在以下行中指定环境的占位符值
const clusterUrl = "<cluster_url>";
const ldapUsername = "<ldap_username>";
const ldapPassword = "<ldap_password>";
const authMechanism = "PLAIN";
// Connection URI连接URI
const uri = `mongodb+srv://${ldapUsername}:${ldapPassword}@${clusterUrl}/?authMechanism=${authMechanism}`;
const client = new MongoClient(uri);
// Function to connect to the server连接到服务器的功能
async 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 Documentation文档
To learn more about any of the methods or types discussed on this page, see the following API documentation:要了解有关本页中讨论的任何方法或类型的更多信息,请参阅以下API文档: