【发布时间】:2020-06-10 18:47:25
【问题描述】:
我正在尝试构建一个网站,让我公司的员工可以输入他们的 Windows 域凭据进行登录。我正在运行一个 Express 后端,如下所示:
const express = require('express');
const bodyParser = require('body-parser');
const passport = require('passport');
const LdapStrategy = require('passport-ldapauth');
// initialize server
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false,
}));
// initialize passport
app.use(passport.initialize());
// define Active Directory connection settings
const getOptions = (request, callback) => {
process.nextTick(() => {
const username = request.query.username;
const password = request.query.password;
const options = {
server: {
url: 'LDAP://internal.mycompany.com',
bindDN: username + '@internal.mycompany.com',
bindCredentials: password,
searchBase: 'DC=internal,DC=mycompany,DC=com',
searchFilter: '(samaccountname=' + username + ')',
},
};
callback(null, options);
});
};
// register passport
passport.use(new LdapStrategy(getOptions));
// respond to GET requests with authentication
app.get('/ldap', passport.authenticate('ldapauth', {session: false}), (request, response) => {
response.setHeader('ContentType', 'application/json');
response.send(JSON.stringify({
success: true,
}));
});
// run server on port 3001
app.listen(3001, () => {
console.log('Express server running on port 3001.');
});
我在网络浏览器中输入http://localhost:3001/ldap?username=myusername&password=mypassword,我得到三种不同响应之一。
回复 A:
{"success":true}
这表明一切都按计划进行。伟大的。这种情况大约有 20% 的时间发生。
回复 B:
Error: connect ECONNREFUSED 10.11.10.165:389
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
这可能只是因为我有时会一次又一次地发送垃圾邮件请求。不经常发生。
响应 C:
OperationsError: 00000000: LdapErr: DSID-0C090627, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, vece
at messageCallback (/c/.../node_modules/ldapjs/lib/client/client.js:1419:45)
at Parser.onMessage (/c/.../node_modules/ldapjs/lib/client/client.js:1089:14)
at emitOne (events.js:116:13)
at Parser.emit (events.js:211:7)
at Parser.write (/c/.../node_modules/ldapjs/lib/messages/parser.js:111:8)
at Socket.onData (/c/.../node_modules/ldapjs/lib/client/client.js:1076:22)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
这是最常见的反应。这是一个令人费解的错误。我在网上阅读的所有内容都表明,提供 bindDN 和 bindCredentials 将允许 activedirectory 包进行初始绑定,然后它应该可以工作。
我也尝试了passport-activedirectory 和activedirectory npm 包,但结果非常相似(passport-activedirectory 从来没有工作过,而 activedirectory 有相同的有时工作行为)。我也尝试了activedirectory2,但没有成功。
All these different links 一直以来都有助于实现这一目标,但我现在不明白我做错了什么。特别是当它有时工作时。我的互联网连接非常稳定,所以我认为这不是问题。
问题:
为什么我会看到这种间歇性行为,是否有解决方案?如果没有,我还有什么其他选择?
【问题讨论】:
标签: javascript node.js authentication active-directory