I am new to Azure Active Directory Development. We have few services which need to be accessed by web application and mobile devices(cordova based application).
- For web application we are using ADAL JS.
- For mobile application we are using Azure AD with an Apache Cordova app.
Issue
The authorization token generating with ADAL JS is authorized successfully over the web services and working as expected. But the token generating with Azure AD is throwing respective
Authorization has been denied for this request
Workaround
We tried to get access token using user credentials in C#. We are successful in acquiring access token. Using that access token we tried to hit our services through Fiddler. Even then the same error thrown as mentioned above "Authorization
has been denied for this request".
Code Samples
Cordova Code
createContext: function () {
AuthenticationContext.createAsync(authority)
.then(function (context) {
mapp.authContext = context;
mapp.log("Created authentication context for authority URL: " + context.authority);
mapp.acquireToken();
}, mapp.error);
},
acquireToken: function () {
if (mapp.authContext == null) {
mapp.error('Authentication context isn\'t created yet. Create context first');
return;
}
mapp.authContext.acquireTokenSilentAsync(resourceUrl, clientID).then(function (success) {
console.log("INSIDESILENT");
mapp.error("Failed to acquire token: " + success);
console.log("DATA:::: "+success);
}, function () {
mapp.authContext.acquireTokenAsync(resourceUrl, clientID, redirectUrl)
.then(function (authResult) {
mapp.log('Acquired token successfully: ' + pre(authResult));
console.log("DATA:::"+authResult.accessToken);
localStorage.setItem("SSOFlag", "true");
angular.bootstrap(document, ['keurapp']);
userDetail = JSON.parse(localStorage.getItem("userDetails"));
}, function (err) {
mapp.error("Failed to acquire token: " + pre(err));
});
});
}
C# Code
public static string GetAccessToken()
{
AuthenticationContext authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/**tenantName**", true);
UserCredential clientCred = new UserCredential("***USERID***", "***PASSWORD***");
AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource: "***APP ID URI OF WEB APPLICATION***", clientId: "***CLIENT ID NATIVE APP***", userCredential: clientCred);
token = authenticationResult.AccessToken;
return token;
}
Service Samples
[EnableCors(origins: "*", headers: "*", methods: "*")]
[Authorize]
public class RepositoryController : ApiController
{
//With Few methods
}
public void Configuration(IAppBuilder app)
{
ConfigureOAuth(app);
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
Can anyone help us does we are missing something in publishing services or where exactly we are going wrong.