在过去的几天里一直在玩 NodeJS,并且使用 ldapjs 模块在 LDAP 连接上卡住了。背景故事,我尝试连接的 Active-Directory 服务器支持 LDAPv3。
下面是我用来连接 LDAP 服务器的代码:
var ldapClient = ldap.createClient({url: 'ldap://ldap.myhost.com.au:389'}, function (err){
if (err) {
console.log('Failed');
process.exit(1);
}
else
console.log('Through');
});
在给定的示例中,它不包括最后一个回调函数,我尝试了它,因为大多数操作都包含回调,所以我认为我可以有一点运气,但我没有。
我也试过有和没有回调,仍然没有工作。
即使我将 url 更改为无效的,ldapClient 容器也将始终返回 true。
-- 到目前为止,James Thomas 在下面的回答已经解释了这一点--
编辑:
我尝试将用户绑定到 LDAP 并执行搜索查询,如下所示:
ldapClient.bind('cn=drupal,ou=SystemAccount,dc=myhost,dc=com,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=com,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);
});
});
此代码不会在屏幕上打印任何内容,结果变量用于保存返回的搜索方法返回值“true”。
任何人都可以分享它是如何完成的?