I'm trying to build one NodeJS server and planning to use the organization's Microsoft Active Directory for authentication.
I tried the same with many packages (activedirectory, activedirectory2, ldapjs etc.)
But none of them seems to work for me.
I'm supplying the LDAP URL and below is my code.
var ldapjs = require('ldapjs');
var config = { url: 'ldap://mycompany.com/dc=mycompany,dc=com'
,timeout: 10
,reconnect: {
"initialDelay": 100,
"maxDelay": 500,
"failAfter": 5
}
}
var username = "[email protected]";
var password="password";
const ldapClient = ldapjs.createClient(config);
ldapClient.bind(username, password, function (err) {
console.log("Logging data...");
ldapClient.search('dc=mycompany,dc=com', function (err, search) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
search.on('searchEntry', function (err,entry) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
else{
var user = entry.object;
console.log("Done.");
return;
}
});
});
});
Sometimes it works, but for most of the times I keep on getting following error (may be when it chooses a different IP)
Error: connect ETIMEDOUT <ip address>:389
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
What puzzles me is; if I try with the same LDAP URL in my C# application, it works fine.
Is there a difference in the way .Net app uses it than the way NodeJS uses?
Can I change my code in some way to make it work?
Node js is faster and used to build both client and server-side network applications because it can handle higher memory in demands, while Python is not so good choice for memory-intensive apps but an ideal choice for back end applications.
Because this is the first question that pops up in Google's search result, and it took me quite some time to figure out how to use Active Directory Authentication, I'm going to share the solution from This tutorial.
It was very easy to understand and implement comparing to other examples I've found on the internet:
npm install --save activedirectory
// Initialize
var ActiveDirectory = require('activedirectory');
var config = {
url: 'ldap://dc.domain.com',
baseDN: 'dc=domain,dc=com'
};
var ad = new ActiveDirectory(config);
var username = '[email protected]';
var password = 'password';
// Authenticate
ad.authenticate(username, password, function(err, auth) {
if (err) {
console.log('ERROR: '+JSON.stringify(err));
return;
}
if (auth) {
console.log('Authenticated!');
}
else {
console.log('Authentication failed!');
}
});
The most difficult part was to figure out what suffix to use for the username.
I was getting the error:
ERROR: {"lde_message":"80090308: LdapErr: DSID-0C090400, comment: AcceptSecurityContext error, data 52e, v1db1\u0000","lde_dn":null}
Before finally setting the right suffix, for me it was something like:var username = '[email protected]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With