How do you use refresh tokens to request multiple resources using ADAL for Cordova

Viewed 571

I'm following this Cordova & ADAL tutorial. I am able to successfully execute the directory search on my native app. I would like to extend the functionality of the app and add an azure web api resource and consume it in the same app.

I created the azure web api using the VS2015 template, published it to azure, added it as a web application in Azure Active Directory and gave my native application access to it in the "permissions to other applications" section within Azure AD.

I would now like to extend the example directory searcher app to also consume the api without having the user sign in again. From my research, I found that I could consume multiple resources using refresh tokens but I'm unable to figure out how to do this without signing the user in to Azure AD a second time to get a token for the web api resource.

Any examples or help would be much appreciated. Here is my jsFiddle to give you an idea of what I'm trying to do.

// Requests some data from an Azure Web API
requestWebApiData: function (authResult) {
    var req = new XMLHttpRequest();
    var url = webApiResourceUri + "/api/values";

    req.open("GET", url, true);
    req.setRequestHeader('Authorization', 'Bearer ' + authResult.accessToken);

    req.onload = function (e) {
        if (e.target.status >= 200 && e.target.status < 300) {
            // This is where I'd like to get my data back from the Azure Web API
            console.log(JSON.stringify(e.target.response));
            return;
        }
        app.error('API Data request failed: ' + e.target.response);
    };
    req.onerror = function (e) {
        app.error('API Data request failed: ' + e.error);
    }

    req.send();
},
1 Answers

I realise this is an old question, but for anyone else:

Assign the delagated permissions required to your client app (ClientA) in Azure, autheticate with acquireTokenAsync and then request your other resource (ClientB) via acquireTokenSilentAsync providing the resource name of ClientB along with ClientAs Client ID.

this.authContext.acquireTokenSilentAsync(
  clientBResourceUri,
  clientAId
);

This will return you a JWT for clientB and a refresh token which will be stored within the tokenCache. If you get prompted that the user needs to login, make sure you have granted permissions for your ClientA app to access ClientB

Related