获取令牌以访问 Azure 资源

Acquire Token to access Azure Resources

我已经使用 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 应用程序,可以列出与用户对应的所有订阅、资源组和保管库。

请确保在 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>

如果有帮助,请告诉我。