Read objectGUID from active directory

后端 未结 1 1352
甜味超标
甜味超标 2021-01-26 06:41

I\'m trying to get information from AD using node.js. I\'ve tried activedirectory and ldapauth-fork and in general the code works, but if I need some <

1条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-26 07:37

    entryParser is there for that purpose:

    const ActiveDirectory = require('activedirectory');
    
    const config = {
      url: 'LDAP://ldap.example.com',
      baseDN: 'OU=Users,DC=example,DC=com',
      username: 'user@example.com',
      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
      );
    });
    

    0 讨论(0)
提交回复
热议问题