2013-07-22 99 views
11

ところ、ユーザが入力がActive Directoryのユーザー名とパスワードだろうと私はそれが有効かどうかを確認しますNodeJSを使用して、私は、ログイン認証ページを作成していますが、私はノードJS LDAP認証ユーザー

[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 ----'); 
       } 
      }); 
     } 
    }); 

誰かが私が間違っていることについて何か提案がありますか?ここで

UPDATE

は、誰もが今までそれを必要とする場合、私が思いついた解決策は、この場合、

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');}}); 
    } 
+0

を? 2番目のケースでは、2つ以上のオブジェクトが検索パラメータと一致しました。おそらく、1つの一致が予想されます。 –

答えて

8

以下の答えの助けを借りて、あなたはldapClientではなくldapServerが必要であり、公式のコード例doc

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); 
    }); 
    }); 
}); 
+1

私は上記を試したが、次のエラーoSuchObjectError:0000208D:NameErr:DSID-0310020A、問題2001(NO_OBJECT)、データ0、最高の一致: \t 'OU =ユーザー、OU = Newyork、DC = basingloc 、DC = lan ' 接続しますが、検索に失敗します。 [email protected] – Sukh

+0

私はこの解決策を使用していた問題を解決しました。ありがとう@zsong! –

7

@Suk h UPDATEソリューションを投稿していただきありがとうございます。ただし、あなたがあなたのUPDATEに投稿したコードに問題があります。単純なケースでは機能しますが、大きなクエリでは、結果が出力される前にバインド解除されています。私の解決策は、あなたのバインドをsearch.on関数に移すことでした。ここで

はあなたのUPDATEの編集です:

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'] 
    // This attribute list is what broke your solution 
    attributes: ['objectGUID','sAMAccountName','cn','mail','manager','memberOf'] 
}; 

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)); 
        } 
        client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}}); 
       }); 

       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');}}); 
       }); 

       // don't do this here 
       //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');}}); 
} 

は、少なくともこれは、Active Directoryの検索であなたのソリューションを使用しているとき、私が発見したものです。 memberOfのは、私のユースケース内のエントリのLOTを返し、バインド解除は時期尚早で行われていたので、私は次のようなエラーになった:フィルターが実際にサーバに送信された最初のケ​​ースでは、

error: 1__ldap://my.domain.com/,OU=Employees,OU=Accounts,DC=my,DC=domain,DC=com closed 
client disconnected