gpt4 book ai didi

javascript - 尝试使用 ADAL.js AuthenticationContext 获取访问 token 时,access_token 与 id_token 相同吗?

转载 作者:数据小太阳 更新时间:2023-10-29 05:24:45 24 4
gpt4 key购买 nike

我正在使用 Azure AD 验证我的单页应用程序 (Angular4),并为此使用 Adal.js。在登录页面上,我单击一个重定向到 Microsoft AAD 的按钮,成功登录后它重定向回应用程序主页,并收到 id_token。以及来自 JWT 的用户信息。

我需要 access_token用于后端 API 访问,我试图通过 ADAL AuthenticationContext 获取的 getCachedToken()方法,并将 clientId 作为参数发送:

this.context.getCachedToken(this.configService.AdalConfig.clientId)

但此方法返回与 id_token (adal.idtoken) 存储在 session 存储中的相同 token .它基本上通过一个连接键在 session 存储中创建一个新项目,该键与 id_token 具有相同的值。

adal.access_token.key + clientId = id_token

例如:adal.access_token.key239f6fc7-64d2-3t04-8gfd-501efc25adkd = <id-token-value> .

我还尝试获取 access_tokenAuthenticationContext.acquireToken()方法,但它也给出了 id_token回来。

我哪里错了?

编辑:发布代码。我正在调用函数 login() ,登录成功后,尝试通过get accessToken()获取主页中的访问 token 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;
}
}

最佳答案

实际上,经过一些阅读后发现,将 SPA 连接到 Azure AD 需要 OAuth 2.0 隐式授权流程。 Microsoft documentation说:

In this scenario, when the user signs in, the JavaScript front enduses Active Directory Authentication Library for JavaScript (ADAL.JS)and the implicit authorization grant to obtain an ID token (id_token)from Azure AD. The token is cached and the client attaches it to therequest as the bearer token when making calls to its Web API back end,which is secured using the OWIN middleware.

所以,我需要发送到后端 API 的是 id_token 本身,它反过来可以被验证和使用。提供了有关验证的更多信息 here :

Just receiving an id_token is not sufficient to authenticate the user;you must validate the id_token's signature and verify the claims inthe token per your app's requirements. The v2.0 endpoint uses JSON WebTokens (JWTs) and public key cryptography to sign tokens and verifythat they are valid.

You can choose to validate the id_token in clientcode, but a common practice is to send the id_token to a backendserver and perform the validation there. Once you've validated thesignature of the id_token, there are a few claims you will be requiredto verify.

关于javascript - 尝试使用 ADAL.js AuthenticationContext 获取访问 token 时,access_token 与 id_token 相同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47234519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com