Goal:
Obtaining UID=KXH user data from an LDAP server
LDAP address is: ldap://10.233.21.116:389
In the engineering root directory, first npm an LDAP access library Ldpajs
NPM Install Ldapjs
In the engineering root directory, create a app.js
var LDAP = require ("Ldapjs");
Create LDAP client, pass server URL into var client = ldap.createclient ({url: ' ldap://10.203.24.216:389 '});
Create an LDAP query option//filter is the equivalent of the SQL condition var opts = {filter: ' (UID=KXH) ',//Query condition filter, find the UID=KXH user node scope: ' Sub ',//query scope
TIMELIMIT:500//Query timeout}; Bind client to LDAP Server//First parameter: Is user, must be from root node to user node full path//second parameter: User password Client.bind (' uid=supbind,cn=users,dc=tiger,dc= com ', ' 123456 ', function (err, res1) {//Start query//First parameter: Query base path, representative in query user confidence will be carried out under this path, which is started by the root section/second parameter: query options client.se
Arch (' dc=tiger,dc=com ', opts, function (err, res2) {//Query result event response Res2.on (' Searchentry ', function (entry) {
Gets the object of the query var user = Entry.object;
var usertext = json.stringify (user,null,2);
Console.log (Usertext);
});
Res2.on (' Searchreference ', function (referral) {Console.log (' referral: ' + referral.uris.join ());
});
Query Error Event Res2.on (' Error ', function (err) {console.error (' ERROR: ' + err.message '); Unbind operation, must be done client.unbind ();
});
The query ends Res2.on (' End ', function (result) {Console.log (' Search status: ' + Result.status);
Unbind operation, must be done client.unbind ();
});
});
});