Node.js LDAP Query instance sharing _node.js

Source: Internet
Author: User
Tags ldap

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 ();    
    
  });
  
});
 });

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.