我正在创建一个登录身份验证页面,用户将在其中输入活动目录的用户名和密码,并使用NodeJS进行检查,以查看其是否有效,但是我一直在获取
[Error: LDAP Error Bad search filter]
要么
[Error: Search returned != 1 results]
当我尝试搜索用户名和密码时,我的代码如下:
我正在使用:https : //github.com/jeremycx/node-LDAP,假设用户输入的用户名是hhill
var ldap = require('LDAP'); var ldapServer = new ldap({ uri: 'ldap://batman.lan', version: 3}); ldapServer.open(function(error) { if(error) { throw new Error('Cant not connect'); } else { console.log('---- connected to ldap ----'); username = '(cn='+username+')'; ldapServer.findandbind({ base: 'ou=users,ou=compton,dc=batman,dc=lan', filter: username, password: password }, function(error, data) { if(error){ console.log(error); } else { console.log('---- verified user ----'); } }); } });
有人对我做错了什么建议吗?
更新
如果有人需要,这是我想出的解决方案,借助以下答案
var username = request.param('username'); var password = request.param('password'); var ldap = require('ldapjs'); ldap.Attribute.settings.guid_format = ldap.GUID_FORMAT_B; var client = ldap.createClient({ url: 'ldap://batman.com/cn='+username+', ou=users, ou=compton, dc=batman, dc=com', timeout: 5000, connectTimeout: 10000 }); var opts = { filter: '(&(objectclass=user)(samaccountname='+username+'))', scope: 'sub', attributes: ['objectGUID'] }; console.log('--- going to try to connect user ---'); try { client.bind(username, password, function (error) { if(error){ console.log(error.message); client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}}); } else { console.log('connected'); client.search('ou=users, ou=compton, dc=batman, dc=com', opts, function(error, search) { console.log('Searching.....'); search.on('searchEntry', function(entry) { if(entry.object){ console.log('entry: %j ' + JSON.stringify(entry.object)); } }); search.on('error', function(error) { console.error('error: ' + error.message); }); client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}}); }); } }); } catch(error){ console.log(error); client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}}); }
在这种情况下,您需要的ldapClient不是ldapServer,而是官方文档中的示例代码:
ldapClient
ldapServer
var ldap = require('ldapjs'); ldap.Attribute.settings.guid_format = ldap.GUID_FORMAT_B; var client = ldap.createClient({ url: 'ldap://127.0.0.1/CN=test,OU=Development,DC=Home' }); var opts = { filter: '(objectclass=user)', scope: 'sub', attributes: ['objectGUID'] }; client.bind('username', 'password', function (err) { client.search('CN=test,OU=Development,DC=Home', opts, function (err, search) { search.on('searchEntry', function (entry) { var user = entry.object; console.log(user.objectGUID); }); }); });