我已使用ADAL.js获取Azure资源的令牌。
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.15/js/adal.min.js"></script>
为了做到这一点,我写了以下代码:
var endpoints = {
"https://management.core.windows.net": "https://management.core.windows.net"
};
var config = {
clientId: '634c7103-b43e-4384-b345-db0116058ac3',
endpoints: endpoints,
};
var authContext = new AuthenticationContext(config);
function login() {
authContext.popUp = true;
authContext.login();
authContext.handleWindowCallback();
};
function clickme() {
var user = authContext.getCachedUser();
console.log(user);
authContext.acquireToken('https://management.core.windows.net', function (error, token) {
console.log(error);
console.log(token);
});
};
现在,当我在登录后调用clickme()时,收到以下错误消息:令牌更新操作因超时而失败。我错过了什么吗?
我的最终目标是创建一个可以列出与用户对应的所有订阅,资源组和保管库的Web应用程序。
答案 0 :(得分:0)
请确保在AAD将响应发送到redirectUri时调用handleWindowCallback
方法。无论加载redirectUri页面时执行什么代码,都应确保调用handlewindowcallback
方法。
请尝试下面的代码,它在我身边工作正常:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.15/js/adal.min.js"></script>
<script>
var endpoints = {
"https://management.core.windows.net": "https://management.core.windows.net"
};
var config = {
clientId: 'e333d3fe-a73a-4476-8121-8a57f9a972ca',
endpoints: endpoints,
};
var authContext = new AuthenticationContext(config);
authContext.handleWindowCallback();
function login() {
authContext.popUp = true;
authContext.login();
// authContext.handleWindowCallback();
var user = authContext.getCachedUser();
console.log(user);
};
function clickme() {
var user = authContext.getCachedUser();
console.log(user);
authContext.acquireToken('https://management.core.windows.net', function (error, token) {
console.log(error);
console.log(token);
});
};
</script>
<input id="Button1" type="button" value="clickme" onclick="clickme()" />
<input id="Button1" type="button" value="login" onclick="login()" />
</body>
</html>
如果有帮助,请告诉我。