admin管理员组

文章数量:1388844

Been playing around with NodeJS in the past few days and run stuck with LDAP connection using ldapjs module. Background story, the Active-Directory server, that I am trying to connect to, supports LDAPv3.

Below is the code I used to connect to the LDAP server:

var ldapClient = ldap.createClient({url: 'ldap://ldap.myhost.au:389'}, function (err){
  if (err) {
    console.log('Failed');
    process.exit(1);
  }
  else
    console.log('Through');
});

In the given example, it doesn't include the last callback function, I tried it as the mon pattern mentioned that most operations include callback so I thought I could have a bit of luck with it, I didn't.

I have also tried with and without the callback, still didn't work.

The ldapClient container will always return true even when I change the url into an invalid one.

-- Up to this point it has been explained by James Thomas' answer below --

EDIT:

I tried binding the user onto LDAP and do a search query as the snippet below:

ldapClient.bind('cn=drupal,ou=SystemAccount,dc=myhost,dc=,dc=au', '', function (err) {
  if (err) {
    console.log('LDAP binding failed... disconnecting');
    process.exit(1);
  }
});

var opts = {
  scope: 'sub'
};

var result = ldapClient.search('ou=Users,dc=myhost,dc=,dc=au', opts, function(err, res) {
  assert.ifError(err);

  res.on('searchEntry', function(entry) {
    console.log('entry: ' + JSON.stringify(entry));
  });
  res.on('searchReference', function(referral) {
    console.log('referral: ' + referral.uris.join());
  });
  res.on('error', function(err) {
    console.error('error: ' + err.message);
  });
  res.on('end', function(result) {
    console.log('status: ' + result.status);
  });
});

This code prints nothing to the screen and result variable that is used to hold the search method return value returned 'true'.

Anyone could share how it's done?

Been playing around with NodeJS in the past few days and run stuck with LDAP connection using ldapjs module. Background story, the Active-Directory server, that I am trying to connect to, supports LDAPv3.

Below is the code I used to connect to the LDAP server:

var ldapClient = ldap.createClient({url: 'ldap://ldap.myhost..au:389'}, function (err){
  if (err) {
    console.log('Failed');
    process.exit(1);
  }
  else
    console.log('Through');
});

In the given example, it doesn't include the last callback function, I tried it as the mon pattern mentioned that most operations include callback so I thought I could have a bit of luck with it, I didn't.

I have also tried with and without the callback, still didn't work.

The ldapClient container will always return true even when I change the url into an invalid one.

-- Up to this point it has been explained by James Thomas' answer below --

EDIT:

I tried binding the user onto LDAP and do a search query as the snippet below:

ldapClient.bind('cn=drupal,ou=SystemAccount,dc=myhost,dc=,dc=au', '', function (err) {
  if (err) {
    console.log('LDAP binding failed... disconnecting');
    process.exit(1);
  }
});

var opts = {
  scope: 'sub'
};

var result = ldapClient.search('ou=Users,dc=myhost,dc=,dc=au', opts, function(err, res) {
  assert.ifError(err);

  res.on('searchEntry', function(entry) {
    console.log('entry: ' + JSON.stringify(entry));
  });
  res.on('searchReference', function(referral) {
    console.log('referral: ' + referral.uris.join());
  });
  res.on('error', function(err) {
    console.error('error: ' + err.message);
  });
  res.on('end', function(result) {
    console.log('status: ' + result.status);
  });
});

This code prints nothing to the screen and result variable that is used to hold the search method return value returned 'true'.

Anyone could share how it's done?

Share Improve this question edited May 23, 2017 at 12:14 CommunityBot 11 silver badge asked Apr 15, 2015 at 7:03 Chris WijayaChris Wijaya 1,3063 gold badges16 silver badges34 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

The ldapjs.createClient() call doesn't connect to the server when you execute the function, rather sets up the credentials to authentication that you use when calling the methods on the returned "client" object.

For example, you need to do the following:

var client = ldap.createClient({url: 'ldap://ldap.myhost..au:389'})
var entry = {
  cn: 'foo',
  sn: 'bar',
  email: ['[email protected]', '[email protected]'],
  objectclass: 'fooPerson'
};
client.add('cn=foo, o=example', entry, function(err) {
  assert.ifError(err);
});

本文标签: javascriptHow to connect LDAP using ldapjs in NodeJSStack Overflow