NodeJS express护照-ldapauth始终返回“无效的用户名/密码”

时间:2018-10-23 13:09:30

标签: node.js active-directory ldap ldapauth

Am在我的Web应用程序中使用passport-ldapauth npm进行LDAP / AD身份验证 但是始终可以确保用户名和密码正确,但总是得到“无效的用户名/密码”。 这是我的密码

var express = require('express')
var app =  express();
var bodyParser = require('body-parser');
var passport     = require('passport');
var LdapStrategy = require('passport-ldapauth');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(function(req, res, next) {
   res.header("Access-Control-Allow-Origin", "*");
   res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, 
   Content-Type, Accept");
   next();
});



var OPTS = {
  server: {
    "url": "ldap://ip-address:389",
    "bindDN": "cn=admin-username,dc=dcname,dc=com",
    "bindCredentials": "admin-password",
    "searchBase": "dc=dcname,dc=lan",
    "searchFilter": "(sAMAccountName={username})",//I have tried (uid={{username}}) also.
  },
  passReqToCallback : true
};

passport.use(new LdapStrategy(OPTS));

app.post('/api/login', function(req, res, next) {  
passport.authenticate('ldapauth', {session: true}, function(err, user, info) 
{
  console.log("err: " + err); //returns null
  console.log("user: " + user);//returns false
  console.log("info: " + JSON.stringify(info));//returns {"message":"Invalid username/password"}
    if (err) {
      return next(err); // will generate a 500 error
    }
    // Generate a JSON response reflecting authentication status
    if (! user) {
      return res.send({ success : false, message : 'authentication failed' 
      });
    }
    return res.send({ success : true, message : 'authentication succeeded' 
    });
   })(req, res, next);  
});
app.listen(3000, function() {
  console.log('Listening on port 3000...')
});

请帮帮我。

1 个答案:

答案 0 :(得分:0)

问题出在组织单位(OU)上。该用户比我在bindDN中未提及的Users多了一个。而且(sAMAccountName = {{username}})也很重要,因为如果我们给(uid = {{{username}})传递用户名是LDAP AD中的sAMAccountName,它将不起作用。因此,新代码将是。

var OPTS = {
    server: {
      "url": "ldap://ip-address:389",
      "bindDN": "cn=username,ou=Users,ou=user-other-ou,dc=dcname,dc=com",
      "bindCredentials": "password",
      "searchBase": "dc=dcname,dc=lan",
      "searchFilter": "(sAMAccountName={{username}})",
    },  
};

希望它将对某人有所帮助。