gheeres / node-activedirectory

ActiveDirectory is an Node.js ldapjs client for authN (authentication) and authZ (authorization) for Microsoft Active Directory with range retrieval support for large Active Directory installations.
MIT License
534 stars 146 forks source link

Unable to fetch user & groups #214

Open akkitech opened 4 years ago

akkitech commented 4 years ago

I am using node js activedirectory module for AD authentication and fetching users & groups. Able to authenticate successfully but unable to get users & groups using .findUser(), .findUsers(), .findGroups() methods. I've added few users and a group using ADSI editor in windows 10. Any clue what could be going wrong?

Screenshot of ADSI Editor :

image

Code :

var ActiveDirectory = require('activedirectory');
var config = {
    url: 'ldap://localhost',
    baseDN: 'cn=admin,dc=ec2,dc=internal',
    username: 'cn=akshay,cn=testContainer,cn=admin,dc=ec2,dc=internal',
    password: '123456'
};

var ad = new ActiveDirectory(config);

var username = 'cn=akshay,cn=testContainer,cn=admin,dc=ec2,dc=internal';
var password = '123456';

ad.authenticate(username, password, function (err, auth) {
    if (err) {
        console.log('ERROR: ' + JSON.stringify(err));
        return;
    }

    if (auth) {
        console.log('Authenticated!');
        ad.findUser({
            dn: "cn=akshay,cn=testContainer,cn=admin,dc=ec2,dc=internal"
        }, function (err, user) {
            if (err) {
                console.log('ERROR: ' + JSON.stringify(err));
                return;
            }

            if (!user) console.log('User: not found.');
            else console.log(JSON.stringify(user));
        });

    } else {
        console.log('Authentication failed!');
    }
});

Output :

Authenticated! User: not found.

KevinBeckers commented 4 years ago

Did you solve this?

akkitech commented 4 years ago

@KevinBeckers following syntax solved my problem:

ad.findUser("cn=akshay,cn=testContainer,cn=admin,dc=ec2,dc=internal", function(err, user) {
  if (err) {
    console.log('ERROR: ' +JSON.stringify(err));
    return;
  }

  if (! user) console.log('User not found');
  else console.log(JSON.stringify(user));
});

you could also pass username@domain.com in the first parameter