如何按Node.js对Windows AD用户进行身份验证?



我最近需要对Windows AD用户进行身份验证。场景如下

  • 网页在服务器 A 上运行 ( Vue + vue-router (
  • API 接口运行在服务器 B(节点 + 快递(
  • 用户在网页上输入 AD 用户名和 pwd(服务器 A(
  • 将用户名和 pwd 传递给服务器 B 上的 API 接口以进行身份验证
  • Server B auth username & pwd via LDAP(windwos AD(
  • 服务器 B 上的 api 将反馈返回到网页(服务器 A(

那么,是否有任何解决方案可以在服务器B上实现,以通过LDAP身份验证用户名和pwd?

太好了!

我使用了nodespi https://github.com/abbr/nodesspi。

但它仅适用于 Windows 环境。而且似乎您只能通过浏览器直接访问服务器B来获取结果。不传递参数以调用服务器 B 上的 api。

无论如何,这对我来说是一个很好的学习场景。

我找到了解决方案。 请参阅: 节点 JS LDAP 身份验证用户

var ldap = require('ldapjs');
var client = ldap.createClient({
url: 'ldap://ldapserver:port/',
timeout: 5000,
connectTimeout: 10000
});
var opts = {
filter: '(&(cn=*))',
scope: 'sub',
// This attribute list is what broke your solution
attributes:['SamAccountName','dn']
};
console.log('--- going to try to connect user ---');
try {
client.bind(username, password, function (error) { //first need to bind
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');}});
});
}
});
} catch(error){
console.log(error);
client.unbind(function(error) {if(error){console.log(error.message);}       else{console.log('client disconnected');}});
}

请记住,如果您收到"错误~~~:超出大小限制"错误,请使用分页和大小限制参数。

var opts = {
filter: '(objectclass=commonobject)',
scope: 'sub',
paged: true,
sizeLimit: 200
};

相关内容

最新更新