2017-11-11 110 views
4

我使用Azure AD对我的单页应用程序(Angular4)进行身份验证,并使用Adal.js进行验证。在登录页面上,我单击一个重定向到Microsoft AAD的按钮,并在成功登录后重定向回应用程序主页,并从JWT收到id_token和用户信息。access_token在尝试使用ADAL.js AuthenticationContext获取访问标记时与id_token相同?

我需要access_token用于后端API访问,而我试图通过该ADAL AuthenticationContextgetCachedToken()方法来获取并发送的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; 
    } 
} 
+0

你应该张贴您的整个验证码... –

+0

为了验证我只是创造了所有必要的信息的'AdalConfig'对象,如'tenant','clientId','redirectUri'等,然后初始化一个使用'AdalConfig'的新'AuthenticationContext',然后使用初始化上下文的方法。我发布了方法调用。请让我知道还有什么是必需的。 – Rishabh

+0

你在哪里指定你想要拨打的资源?你需要发布你的代码或者没有人能够帮助你。 –

回答

2

其实一点阅读后,原来那SPA的连接到Azure的指令要求的OAuth 2.0隐式授予流程。该Microsoft documentation说:

在这种情况下,当,JavaScript的前端 使用Active Directory身份验证JavaScript库(ADAL.JS)用户登录 和隐含授权授权以获得一个ID令牌(id_token )来自Azure AD的 。当调用其Web API后端 (使用OWIN中间件进行保护)时,令牌被缓存,客户端将其作为不记名令牌附加到 请求中。

因此,我需要将id_token本身发送到后端API,后者又可以进行验证和使用。有关验证的更多信息,请参阅here

只接收到id_token不足以验证用户; 您必须验证id_token的签名并根据您的应用程序的要求验证 令牌中的声明。 v2.0端点使用JSON Web 令牌(JWT)和公钥密码体系对令牌进行签名,并验证 它们是否有效。

您可以选择验证客户端 代码中的id_token,但通常的做法是将id_token发送到后端 服务器并在其中执行验证。一旦您验证了id_token的 签名,就会有一些声明要求您验证 。

+0

你是否设法使用adal从id令牌获取访问令牌? – hngdev

相关问题