我使用Azure AD对我的单页应用程序(Angular4)进行身份验证,并使用Adal.js进行验证。在登录页面上,我单击一个重定向到Microsoft AAD的按钮,并在成功登录后重定向回应用程序主页,并从JWT收到id_token
和用户信息。access_token在尝试使用ADAL.js AuthenticationContext获取访问标记时与id_token相同?
我需要access_token
用于后端API访问,而我试图通过该ADAL AuthenticationContext
的getCachedToken()
方法来获取并发送的clientId作为参数:
this.context.getCachedToken(this.configService.AdalConfig.clientId)
但这种方法会返回存储在会话存储中的相同标记为id_token (adal.idtoken)
。 adal.access_token.key239f6fc7-64d2-3t04-8gfd-501efc25adkd = <id-token-value>
:它基本上与级联键,它具有相同的值,id_token
adal.access_token.key + clientId = id_token
前创建会话存储的新项目。
我也尝试用AuthenticationContext.acquireToken()
方法取access_token
方法,但它也给了id_token
回来。
我哪里错了?
编辑:张贴代码。 我在调用函数login()
,并成功登录后,试图通过 adal.config.ts
中的get accessToken()
属性访问器获取主页中的访问令牌。
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'对象,如'tenant','clientId','redirectUri'等,然后初始化一个使用'AdalConfig'的新'AuthenticationContext',然后使用初始化上下文的方法。我发布了方法调用。请让我知道还有什么是必需的。 – Rishabh
你在哪里指定你想要拨打的资源?你需要发布你的代码或者没有人能够帮助你。 –