无法使用 passport-ldapauth 绑定 AD 管理员

Unable to bind AD admin using passport-ldapauth

尝试配置 passport-ldapauth 和测试身份验证时,出现以下错误:

lde_message: "000004DC: LdapErr: DSID-0C0907C2, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v2580"

我假设这是因为不允许匿名绑定,我已经向 passport-ldapauth 的选项提供了管理员帐户凭据...

/* /src/app.ts */

import * as express from "express";
import * as bodyParser from 'body-parser';
import * as session from 'express-session';
import * as helmet from 'helmet';
import * as path from 'path';
import * as uuid from 'uuid/v4';
import * as passport from 'passport';
import * as ldapAuthStrategy from 'passport-ldapauth'
const OPTS: ldapAuthStrategy.Options = {
  server: {
    url: process.env.AUTH_LDAP_URL,
    bindDN: process.env.AUTH_LDAP_BIND_USER_DN,
    bindCredentials: process.env.AUTH_LDAP_BIND_PASS,
    searchBase: "DC=example,DC=com,DC=us",
    searchFilter: "(sAMAccountName={{username}})"
  }
}
passport.use(new ldapAuthStrategy(OPTS));

我的 env 文件看起来像这样...

AUTH_LDAP_URL='ldap://X.X.X.X:389'
AUTH_LDAP_BIND_USER_DN='cn=Administrator,dc=example,dc=com,dc=us'
AUTH_LDAP_BIND_PASSWORD='PASSWORD'

在路由中,我要求进行身份验证,这是发出错误的地方...

/* /src/routes/login/index.ts */
import * as express from 'express';
import * as passport from 'passport';
const Router = express.Router();

Router.get('/AD/', (req, res)=>{
  res.render('login/login', {req: req, title: 'Login'});
}).post('/AD/go',  (req: express.Request, res: express.Response, next: express.NextFunction): void | Response => {
  passport.authenticate('ldapauth', (err, user, info): void => {
    var error = err || info
    if (error){
      res.status(500).json({ status: 500, data: error })
    }else if (!user){
      res.status(401).json({ status: 404, data: "User Not Found" })
    }else{
      res.status(500).json({ status: 200, data: user })
    }
  })(req, res, next)
});

module.exports = Router;

我花了 7 个小时阅读文档并尝试在线查找示例以帮助阐明这个问题。一些值已被修改以编辑数据。提前致谢。

编辑: 如果我使用 ldapsearch(再次编辑)

ldapsearch -b "dc=example,dc=com,dc=us" -H ldap://X.X.X.X:389 -x -D "cn=Administrator,dc=example,dc=com,dc=us" -W '(sAMAccountName=testuser)'
...
# search result
search: 2
result: 0 Success

您的用法看起来是正确的,所以也许检查您的设置并验证一切是否正确。

  • AUTH_LDAP_BIND_USER_DN真的正确吗?我知道您可能针对这个问题更改了此设置,但您显示的 DN 将用户置于域的根目录,这可能并非如此。确保您已从用户的 distinguishedName 属性中提取该值。
  • 当然,请检查密码。
  • 你的web服务器真的能连接到LDAP服务器的389端口吗?如何测试取决于您的服务器 OS 运行。我会经常从命令行使用 telnet 客户端:telnet X.X.X.X 389

核心问题是环境变量输入错误。 AUTH_LDAP_BIND_PASS 应该是 AUTH_LDAP_BIND_PASSWORD