Acquire Token to access Azure Resources

570 views Asked by At

I have used the ADAL.js for acquiring token for Azure Resources.

<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.15/js/adal.min.js"></script>

I have written the following code in order to do so:

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

Now when I have called clickme() after login, got the following error message: Token renewal operation failed due to timeout. Am I missing something ?

My final goal is to create a web application that can list all the subscription, resource group and vault corresponding to the user.

1

There are 1 answers

3
Nan Yu On BEST ANSWER

Please ensure that handleWindowCallback method is called when AAD sends the response to redirectUri. Whatever code is executed when the redirectUri page is loaded should ensure that handlewindowcallback method is called.

Please try below code , it works fine on my side :

<!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>

Please let me know if it helps.