如何使用Nodejs从Azure AD连接和查找用户

时间:2018-03-29 13:00:18

标签: node.js azure active-directory azure-active-directory ldapjs

我正在尝试使用nodejs从azure活动目录中查找用户。我正在使用Node-ActiveDirectory执行此任务。首先,我尝试连接到Azure活动目录,作为above link中的给定示例。我用过的示例代码如下。

var ActiveDirectory = require('activedirectory');
var config = { url: 'ldap://myhotmail.onmicrosoft.com',
    baseDN: 'dc=myhotmail,dc=onmicrosoft,dc=com',
    username: 'roledene@myhotmail.onmicrosoft.com',
    password: 'myPassword'
}
var ad = new ActiveDirectory(config);

var username = 'roledene@myhotmail.onmicrosoft.com';
var password = 'myPassword';

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!');
    }
});

但它会出现以下错误。这段代码出了什么问题?

ERROR: {"code":"ENOTFOUND","errno":"ENOTFOUND","syscall":"getaddrinfo","hostname":"myhotmail.onmicrosoft.com","host":"myhotmail.onmicrosoft.com","port":389}

1 个答案:

答案 0 :(得分:0)

作为@juunas评论,我尝试使用Microsoft Graph客户端。

我将引用设置ms图形客户端的初始步骤,详细信息请参见here以获取更多详细信息

安装ms图形客户端

npm install @microsoft/microsoft-graph-client

从MS graph api

连接和查询
const MicrosoftGraph = require("@microsoft/microsoft-graph-client");

var client = MicrosoftGraph.Client.init({
    authProvider: (iss, sub, profile, accessToken, refreshToken, done) => {
        done(null, {
            profile,
            accessToken,
            refreshToken
        }); //first parameter takes an error if you can't get an access token
    }
});

// Example calling /me with no parameters
client
    .api('/me')
    .get((err, res) => {
        console.log(res); // prints info about authenticated user
    });