I am authenticating my Single Page App (Angular4) with Azure AD, and using Adal.js for the same. On the login page, I click a button that redirects to Microsoft AAD and upon successful login it redirects back to application home page, and receives id_token
and user info from JWT.
I need the access_token
for back-end API access, which I am trying to acquire through the the ADAL AuthenticationContext
's getCachedToken()
method, and sending the clientId as parameter:
this.context.getCachedToken(this.configService.AdalConfig.clientId)
But this method returns the same token which is stored in session storage as id_token (adal.idtoken)
. It basically creates a new item in session storage by with a concatenated key, which has same value as id_token
adal.access_token.key + clientId = id_token
ex: adal.access_token.key239f6fc7-64d2-3t04-8gfd-501efc25adkd = <id-token-value>
.
I also tried to fetch access_token
with AuthenticationContext.acquireToken()
method, but it too gave the id_token
back.
Where am I going wrong?
EDIT: posting the code.
I am calling the function login()
, and after successful login, trying to get the access token in home page via get accessToken()
property accessor in adal.config.ts
.
config.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class ConfigService {
constructor() {}
public get AdalConfig(): any {
return {
tenant: 'common',
clientId: <application-id>,
redirectUri: window.location.origin + '/',
postLogoutRedirectUri: window.location.origin + '/'
};
}
}
adal.service.ts
import { ConfigService } from './config.service';
import { Injectable } from '@angular/core';
import { adal } from 'adal-angular';
let createAuthContextFn: adal.AuthenticationContextStatic = AuthenticationContext;
@Injectable()
export class AdalService {
private context: adal.AuthenticationContext;
constructor(private configService: ConfigService) {
this.context = new createAuthContextFn(configService.AdalConfig);
}
login() {
this.context.login();
}
logout() {
this.context.logOut();
}
handleCallback() {
this.context.handleWindowCallback();
}
public get userInfo() {
return this.context.getCachedUser();
}
public get accessToken() {
return this.context.getCachedToken(this.configService.AdalConfig.clientId);
// return this.context.acquireToken(this.configService.AdalConfig.clientId, function(message, token, response) {
// console.log(message, token, response);
// });
}
public get isAuthenticated() {
return this.userInfo && this.accessToken;
}
}
AdalConfig
object with all the required info such astenant
,clientId
,redirectUri
etc, and then initializing a newAuthenticationContext
using theAdalConfig
, and then using the initialized context's methods. I have posted the method call. Please let me know what else is required. – Rishabhconfig.service.ts
andadal.service.ts
code, that's where I am specifying all the required info by AAD. Hope this will help :) – Rishabh