I built an Cordova App for ios/android. The app uses an Azure mobile service tied to Azure Active Directory. This works well. When I try to do a directory search, I am prompted by Azure to authenticate using my tenant credentials.
Next steps:
We have few apis which has some C.R.U.D on an Azure SQL database. The API works fine and I get data from it (while unsecured) by navigating to https://mat.azurewebsites.net/api/values.
Once the user has authenticated using the ADAL Cordova library (working just fine and I receive a token back), I would like to execute a GET request to my Web API and return data.
This is where I'm having trouble and not sure how to construct the request to the API so it
We have similar question posted here How do I query Azure Web API from Cordova Azure Authenticated app
But no answer there.
I need to call multiple apis.... In AngularJs adal we can pass multiple endpoints but here I don't know that how we can pass.
var authority = "https://login.microsoftonline.com/TenantId",
redirectUri = "https://Mobile",
resourceUri = "https://graph.microsoft.com" // I am not sure what should be here,
clientId = "xxxxxxxxxxxxxxx";
var url = "https://xxxxx-api.cloudapp.net/v1/purchaseorders?
$orderby=OrderPlacementDate desc"
req.open("GET", url, true);
req.setRequestHeader('Authorization', 'Bearer ' +
authResult.accessToken);
req.setRequestHeader('XXX.FunctionGroup', 'PurchaseProduct');
req.onload = function (e) {
if (e.target.status >= 200 && e.target.status < 300) {
app.error('Valid');
app.renderData(JSON.parse(e.target.response));
return;
}
app.error('Data request failed error: ' + e.target.response + '.......' + e.target.status);
};
req.onerror = function (e) {
app.error('Data request failed onerror: ' + e);
}
req.send();
@@@@@@ this is the code for authenticate
authenticate: function (authCompletedCallback) {
app.context = new Microsoft.ADAL.AuthenticationContext(authority);
app.context.tokenCache.readItems().then(function (items) {
if (items.length > 0) {
authority = items[0].authority;
app.context = new Microsoft.ADAL.AuthenticationContext(authority);
}
// Attempt to authorize user silently
app.context.acquireTokenSilentAsync(resourceUri, clientId)
.then(authCompletedCallback, function () {
// We require user cridentials so triggers authentication dialog
app.context.acquireTokenAsync(resourceUri, clientId, redirectUri)
.then(authCompletedCallback, function (err) {
app.error("Failed to authenticate: " + err);
});
});
});
}