我正在尝试使用 node.js 从 AD 获取信息。我试过了activedirectory
and ldapauth-fork
一般来说,代码可以工作,但如果我需要一些octetstring
数据如objectGUID
,我在对象中看到了垃圾字符串。我found https://github.com/mcavage/node-ldapjs/issues/228将二进制数据转换为 utf-8 字符串。但问题是数据在转换过程中被损坏(很多带有 65533 代码的 cahr),并且我无法将字符串恢复为原始二进制文件。
我如何访问其中的数据octetstring
格式以获得正确的二进制表示形式?
const ActiveDirectory = require('activedirectory');
const config = {
url: 'LDAP://ldap.example.com',
baseDN: 'OU=Users,DC=example,DC=com',
username: '[email protected] /cdn-cgi/l/email-protection',
password: 'password'
};
const ad = new ActiveDirectory(config);
const query = {
filter: '(objectClass=user)',
attributes: ["dn", "cn", "objectGUID", "objectSid"]
};
ad.findUsers(query, function (err, result) {
if (err) {
return console.error(err);
}
console.log(result.length);
console.log(result[0]); // objectGUID contains rubbish
console.log([...result[0].objectGUID].map(ch => ch.charCodeAt(0)));
});
Related:
- https://github.com/mcavage/node-ldapjs/issues/228 https://github.com/mcavage/node-ldapjs/issues/228
- https://github.com/gheeres/node-activedirectory/pull/15 https://github.com/gheeres/node-activedirectory/pull/15
entryParser
是为了这个目的:
const ActiveDirectory = require('activedirectory');
const config = {
url: 'LDAP://ldap.example.com',
baseDN: 'OU=Users,DC=example,DC=com',
username: '[email protected] /cdn-cgi/l/email-protection',
password: 'password',
entryParser(entry, raw, callback) {
if (raw.hasOwnProperty("objectGUID")) { entry.objectGUID = raw.objectGUID; }
callback(entry);
}
};
const ad = new ActiveDirectory(config);
const query = {
filter: '(objectClass=user)',
attributes: ["dn", "cn", "objectGUID", "objectSid"]
};
ad.findUsers(query, function (err, result) {
if (err) {
return console.error(err);
}
console.log(result.length);
console.log(result[0]); // objectGUID contains Buffer with strange byte order
console.log(result[0].objectGUID
.toString('hex')
.replace(
/^(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)$/,
"{$4$3$2$1-$6$5-$8$7-$10$9-$16$15$14$13$12$11}"
).toUpperCase() // Normal guid, conversion could be moved into the parser
);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)