A30*_*006 12 active-directory node.js
我正在尝试构建一台 NodeJS 服务器并计划使用该组织的 Microsoft Active Directory 进行身份验证。
我对许多软件包(activedirectory、activedirectory2、ldapjs 等)尝试了同样的方法
但它们似乎都不适合我。
我提供了 LDAP URL,下面是我的代码。
var ldapjs = require('ldapjs');
var config = { url: 'ldap://mycompany.com/dc=mycompany,dc=com'
,timeout: 10
,reconnect: {
"initialDelay": 100,
"maxDelay": 500,
"failAfter": 5
}
}
var username = "user_id@mycompany.com";
var password="password";
const ldapClient = ldapjs.createClient(config);
ldapClient.bind(username, password, function (err) {
console.log("Logging data...");
ldapClient.search('dc=mycompany,dc=com', function (err, search) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
search.on('searchEntry', function (err,entry) {
if (err) {
console.log('ERROR: ' +JSON.stringify(err));
return;
}
else{
var user = entry.object;
console.log("Done.");
return;
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
有时它有效,但在大多数情况下,我不断收到以下错误(可能是当它选择不同的 IP 时)
Error: connect ETIMEDOUT <ip address>:389
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
Run Code Online (Sandbox Code Playgroud)
令我困惑的是;如果我在我的 C# 应用程序中尝试使用相同的 LDAP URL,它工作正常。
.Net 应用程序使用它的方式与 NodeJS 使用的方式有什么不同吗?
我可以以某种方式更改我的代码以使其工作吗?
因为这是 Google 搜索结果中弹出的第一个问题,而且我花了很长时间才弄清楚如何使用 Active Directory 身份验证,所以我将分享本教程中的解决方案。
与我在互联网上找到的其他示例相比,它很容易理解和实现:
npm install --save activedirectory
// Initialize
var ActiveDirectory = require('activedirectory');
var config = {
url: 'ldap://dc.domain.com',
baseDN: 'dc=domain,dc=com'
};
var ad = new ActiveDirectory(config);
var username = 'john.smith@domain.com';
var password = 'password';
// Authenticate
ad.authenticate(username, password, function(err, auth) {
if (err) {
console.log('ERROR: '+JSON.stringify(err));
return;
}
if (auth) {
console.log('Authenticated!');
}
else {
console.log('Authentication failed!');
}
});
Run Code Online (Sandbox Code Playgroud)
最困难的部分是弄清楚用户名使用什么后缀。
我收到错误:
错误:{“lde_message”:“80090308:LdapErr:DSID-0C090400,评论:AcceptSecurityContext 错误,数据 52e,v1db1\u0000”,“lde_dn”:null}
在最终设置正确的后缀之前,对我来说是这样的:
var username = 'john.smith@foo.companyname.com