如何使用 Nodejs 从 Azure AD 连接和查找用户
How to connect and find a user from Azure AD using Nodejs
我正在尝试使用 nodejs 从 azure 活动目录中查找用户。我正在使用 Node-ActiveDirectory for this task. First I tried to connect to the Azure active directory as the given example in the 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}
作为@juunas 的评论,我尝试使用 Microsoft Graph 客户端。
我将引用 here 中完全提到的设置 ms 图形客户端的初始步骤以获取更多详细信息
正在安装 ms graph 客户端
npm install @microsoft/microsoft-graph-client
从 MS 图连接和查询 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
});
我正在尝试使用 nodejs 从 azure 活动目录中查找用户。我正在使用 Node-ActiveDirectory for this task. First I tried to connect to the Azure active directory as the given example in the 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}
作为@juunas 的评论,我尝试使用 Microsoft Graph 客户端。
我将引用 here 中完全提到的设置 ms 图形客户端的初始步骤以获取更多详细信息
正在安装 ms graph 客户端
npm install @microsoft/microsoft-graph-client
从 MS 图连接和查询 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
});