Overview
You can authenticate to a Lightweight Directory Access Protocol (LDAP) server by using your directory server username and password.
Tip
PLAIN Authentication
The authentication mechanism is named PLAIN instead of LDAP because
the driver authenticates by using the PLAIN Simple Authentication and Security Layer
(SASL) defined in RFC-4616.
Specify LDAP Authentication
The examples in this section show how to specify LDAP authentication
by using either a connection string or the MongoCredential class. Replace
the following placeholder values:
<LDAP username>: Your LDAP username.<password>: Your LDAP password.<hostname>: The network address of your MongoDB deployment, accessible by your client.<port>: The port number of your MongoDB deployment. If you omit this parameter, the driver uses the default port number (27017).
Select the Connection String or the MongoCredential tab below for instructions and sample code for specifying this authentication mechanism:
To specify the LDAP authentication mechanism by using a connection
string:
Assign the
authMechanismURL parameter to the valuePLAIN(Optional) Assign the
authSourceURL parameter to the value$external
Note
If you specify the PLAIN mechanism, you cannot assign
authSource to any value other than $external.
The following code specifies the authentication mechanism in a connection string:
val connectionString = ConnectionString("<LDAP username>:<password>@<hostname>:<port>/?authSource=$external&authMechanism=PLAIN") val mongoClient = MongoClient.create(connectionString)
To specify the LDAP authentication mechanism by using the
MongoCredential class, use the createPlainCredential()
method as shown in the following example:
val credential = MongoCredential.createPlainCredential("<LDAP username>", "$external", "<password>".toCharArray()) val settings = MongoClientSettings.builder() .applyToClusterSettings { builder -> builder.hosts(listOf(ServerAddress("<hostname>", <port>))) } .credential(credential) .build() val mongoClient = MongoClient.create(settings)
Additional Information
To learn more about authenticating to MongoDB, see Authentication in the MongoDB Server manual.
To learn more about creating a MongoClient object by using the
Kotlin Sync driver, see the Create a MongoClient guide.
API Documentation
To learn more about the classes and methods for authenticating your application with Kotlin Sync driver, see the following API documentation: