使用 MSAL.js 遍历租户
Looping through tenants using MSAL.js
我正在使用 MSAL.js 登录并使用图表 API.
我创建的 Azure AD 应用程序是一个多租户,并且在 运行 针对特定租户时具有正确的权限,因此我可以确认它是否有效。
障碍是我希望能够遍历所有租户以获取图表中可用的任何数据 API。
我曾尝试简单地更改 MSAL 连接的配置,但是失败了,每次调用都会使用现有的 authority
值。
在此之后,我创建了一个 forEach
数组,其中 tenantid
用于循环,但是这产生了相同的结果。
我创建了一个每次都会调用的注销函数,但是这会将您从您的页面重定向到您的注销地址。
我的代码:
function config_app(tenantid, callback, apiUrl) {
var applicationConfig = {
auth: {
clientId: "XXXX-XXXX-XXXX-XXXX",
authority: "https://login.microsoftonline.com/" + tenantid,
redirectUri: "https://my.redirecturi.com/fake"
},
cache: {
cacheLocation: "sessionStorage",
storeAuthStateInCookie: false
}
};
var msalInstance = new Msal.UserAgentApplication(applicationConfig);
callback(tenantid, applicationConfig, msalInstance, callMSGraph, apiUrl);
}
function sign_in(tenantid, applicationConfig, msalInstance, callback, apiUrl) {
var scopes = {
scopes: ["Organization.Read.All"],
loginHint: "my@email.com"
};
msalInstance.acquireTokenSilent(scopes).then(response => {
callback(response.accessToken, graphAPICallback, apiUrl);
}).catch(err => {
});
}
function callMSGraph(accessToken, callback, apiUrl) {
console.log("calling ms graph");
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200)
callback(JSON.parse(this.responseText));
}
xmlHttp.open("GET", "https://graph.microsoft.com/v1.0/" + apiUrl, true);
xmlHttp.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xmlHttp.send();
}
function graphAPICallback(data) {
$('#o365res').append(JSON.stringify(data, null, 2));
}
config_app('XXX-XXX-XXX-XXX-XXX', sign_in, 'organization');
我尝试将一些租户 ID 添加到数组中并循环使用:
var clients = ['XXX-XXX-XXX-XXX-XXX','YYYY-YYYY-YYYY-YYYY'];
clients.forEach(function(e) {
config_app(e, sign_in, 'organization');
});
经过大量阅读,我似乎无法在没有重定向的情况下使用注销功能,我更愿意能够通过 authority
值简单地更改租户并使用相同的令牌跳转。
我正在使用 MSAL v1.2.0
我希望从我们的租户中提取所有组织数据以显示在单个页面上,然后将其扩展以从 API.[=18= 中提取安全分数和任何其他有价值的数据]
谢谢
在与 MSAL.js 的开发人员联络后,我完成了我的解决方案。
GitHub 个问题:
They said it was related/duplicate of this
不过我发现通过简化函数并在作用域中添加 forceRefresh: true,
,每个 pass 都成功地更改了权限并拉取了所需的数据。
这是我的功能:
function getData(tenantid,apiUrl) {
return new Promise(resolve => {
var applicationConfig = {
auth: {
clientId: "AZURE-APP-ID-HERE",
authority: "https://login.microsoftonline.com/" + tenantid,
redirectUri: "https://my.redirect.uri/uri"
},
cache: {
cacheLocation: "localStorage",
storeAuthStateInCookie: false
}
};
var msalInstance = new Msal.UserAgentApplication(applicationConfig);
var scopes = {
forceRefresh: true,
scopes: ["Organization.Read.All"],
loginHint: "user@email.com"
};
msalInstance.acquireTokenSilent(scopes).then(response => {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText);
$('#o365res').append(JSON.stringify(data, null, 2));
resolve();
}
}
xmlHttp.open("GET", "https://graph.microsoft.com/v1.0/" + apiUrl, true);
xmlHttp.setRequestHeader('Authorization', 'Bearer ' + response.accessToken);
xmlHttp.send();
}).catch(err => { });
});
}
function getDataChain(clients) {
const nextClient = clients.shift();
if (nextClient) {
return getData(nextClient,'organization').then(_ => getDataChain(clients))
} else { return Promise.resolve(); }
}
后面是我的客户端 ID 数组并调用链函数:
var clients = ['XXX-XXX-XXX-XXX-XXX','YYY-YYY-YYY-YYY-YYY'];
getDataChain(clients).then(_ => console.log("all finished"));
我正在使用 MSAL.js 登录并使用图表 API.
我创建的 Azure AD 应用程序是一个多租户,并且在 运行 针对特定租户时具有正确的权限,因此我可以确认它是否有效。
障碍是我希望能够遍历所有租户以获取图表中可用的任何数据 API。
我曾尝试简单地更改 MSAL 连接的配置,但是失败了,每次调用都会使用现有的 authority
值。
在此之后,我创建了一个 forEach
数组,其中 tenantid
用于循环,但是这产生了相同的结果。
我创建了一个每次都会调用的注销函数,但是这会将您从您的页面重定向到您的注销地址。
我的代码:
function config_app(tenantid, callback, apiUrl) {
var applicationConfig = {
auth: {
clientId: "XXXX-XXXX-XXXX-XXXX",
authority: "https://login.microsoftonline.com/" + tenantid,
redirectUri: "https://my.redirecturi.com/fake"
},
cache: {
cacheLocation: "sessionStorage",
storeAuthStateInCookie: false
}
};
var msalInstance = new Msal.UserAgentApplication(applicationConfig);
callback(tenantid, applicationConfig, msalInstance, callMSGraph, apiUrl);
}
function sign_in(tenantid, applicationConfig, msalInstance, callback, apiUrl) {
var scopes = {
scopes: ["Organization.Read.All"],
loginHint: "my@email.com"
};
msalInstance.acquireTokenSilent(scopes).then(response => {
callback(response.accessToken, graphAPICallback, apiUrl);
}).catch(err => {
});
}
function callMSGraph(accessToken, callback, apiUrl) {
console.log("calling ms graph");
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200)
callback(JSON.parse(this.responseText));
}
xmlHttp.open("GET", "https://graph.microsoft.com/v1.0/" + apiUrl, true);
xmlHttp.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xmlHttp.send();
}
function graphAPICallback(data) {
$('#o365res').append(JSON.stringify(data, null, 2));
}
config_app('XXX-XXX-XXX-XXX-XXX', sign_in, 'organization');
我尝试将一些租户 ID 添加到数组中并循环使用:
var clients = ['XXX-XXX-XXX-XXX-XXX','YYYY-YYYY-YYYY-YYYY'];
clients.forEach(function(e) {
config_app(e, sign_in, 'organization');
});
经过大量阅读,我似乎无法在没有重定向的情况下使用注销功能,我更愿意能够通过 authority
值简单地更改租户并使用相同的令牌跳转。
我正在使用 MSAL v1.2.0
我希望从我们的租户中提取所有组织数据以显示在单个页面上,然后将其扩展以从 API.[=18= 中提取安全分数和任何其他有价值的数据]
谢谢
在与 MSAL.js 的开发人员联络后,我完成了我的解决方案。
GitHub 个问题:
They said it was related/duplicate of this
不过我发现通过简化函数并在作用域中添加 forceRefresh: true,
,每个 pass 都成功地更改了权限并拉取了所需的数据。
这是我的功能:
function getData(tenantid,apiUrl) {
return new Promise(resolve => {
var applicationConfig = {
auth: {
clientId: "AZURE-APP-ID-HERE",
authority: "https://login.microsoftonline.com/" + tenantid,
redirectUri: "https://my.redirect.uri/uri"
},
cache: {
cacheLocation: "localStorage",
storeAuthStateInCookie: false
}
};
var msalInstance = new Msal.UserAgentApplication(applicationConfig);
var scopes = {
forceRefresh: true,
scopes: ["Organization.Read.All"],
loginHint: "user@email.com"
};
msalInstance.acquireTokenSilent(scopes).then(response => {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText);
$('#o365res').append(JSON.stringify(data, null, 2));
resolve();
}
}
xmlHttp.open("GET", "https://graph.microsoft.com/v1.0/" + apiUrl, true);
xmlHttp.setRequestHeader('Authorization', 'Bearer ' + response.accessToken);
xmlHttp.send();
}).catch(err => { });
});
}
function getDataChain(clients) {
const nextClient = clients.shift();
if (nextClient) {
return getData(nextClient,'organization').then(_ => getDataChain(clients))
} else { return Promise.resolve(); }
}
后面是我的客户端 ID 数组并调用链函数:
var clients = ['XXX-XXX-XXX-XXX-XXX','YYY-YYY-YYY-YYY-YYY'];
getDataChain(clients).then(_ => console.log("all finished"));