【问题标题】:LDAP authentication using ldapjs in nodejs在 nodejs 中使用 ldapjs 进行 LDAP 身份验证
【发布时间】:2020-05-28 12:27:43
【问题描述】:

我是 node.js 的新手,对 LDAP 身份验证有所了解。这里我试图从搜索中检索员工 ID,但是尽管传递的凭据已成功绑定,但没有获取任何搜索条目,不确定我在哪里误导。如果有人可以帮助我,那将是一个很大的帮助!

下面是代码sn-p的结果集:

读卡器绑定成功 搜索结果长度:0 搜索 retval:{"messageID":2,"protocolOp":"LDAPResult","status":0,"matchedDN":"","errorMessage":"","re​​ferrals":[],"controls":[] } 没有要绑定的唯一用户

ldapRoute.route('/ldap').post((req, res, next) => {	

	var result = "";
	 
	var email =req.body.email;

	var client = ldap.createClient({
        url: 'ldap://******'
	});

	var opts = {
		filter: '(sAMAccountName='+ email + ')',
		attributes: ['sAMAccountName']
		};
		
        
var username = 'ii' + "\\" + email;
	
client.bind(username, req.body.password, function(err) {
    if (err){
		result += "Reader bind failed " + err;
    res.send(result);
    return;
	
	}
	else{
    
    result += "Reader bind succeeded\n";
	}
	
	client.search('OU=emp,dc=i,dc=ac,dc=com', opts, function(err, searchRes) {
		
	var searchList = []

	if (err) {
    result += "Search failed " + err;
    res.send(result);
    return;
	}
	
searchRes.on("searchEntry", (entry) => {
    result += "Found entry: " + entry + "\n";
    searchList.push(entry);
	
	});
	
searchRes.on("error", (err) => {
    result += "Search failed with " + err;
    res.send(result);
	
	});
	
searchRes.on("end", (retVal) => {
    result += "Search results length: " + searchList.length + "\n";
    for(var i=0; i<searchList.length; i++)
    result += "DN:" + searchList[i].employeeID + "\n";
    result += "Search retval:" + retVal + "\n";
	
  if (searchList.length == 1)   {
    client.bind(searchList[0].employeeID, req.body.password, function(err) {
      if (err)
         result += "Bind with real credential error: " + err;
      else
        result += "Bind with real credential is a success";
		
		   res.send(result);
		});  // client.bind (real credential)
		
		} else { 
                        result += "No unique user to bind";
                        res.send(result);
                    }

         });  

		});  

 }); 

});

【问题讨论】:

    标签: node.js express ldapjs


    【解决方案1】:

    问题出在过滤器中,由于一些奇怪的原因,“end”在点击“searchEntry”之前就被触发了,调试它帮助我解决了这个问题。

    //Filter
    var opts = {
     filter: '(sAMAccountName=' + email+')',
     scope: 'sub',
     attributes: ['employeeID']
     }; 
    
    //Search
    client.search('OU=empl,dc=ii,dc=ac,dc=in', opts, function(err, searchRes) 
    {
       if (err) 
     {   
       result += "Search failed " + err;    
       res.send(result); 
       return;
     }else{
    searchRes.on("searchEntry", (entry) => 
     {
       result += "Found entry: " + entry.object.employeeID;
       res.send(result);
     }
     / ........../
    } });
    

    【讨论】:

      猜你喜欢
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-03
      • 2014-03-08
      • 2014-08-09
      • 2011-07-23
      • 1970-01-01
      相关资源
      最近更新 更多