javascript – 在尝试使用ADAL.js AuthenticationContext获取访问令牌时,access_token与id_token相同?

我正在使用Azure AD对我的单页应用程序(Angular4)进行身份验证,并使用Adal.js进行身份验证.在登录页面上,我单击一个重定向到Microsoft AAD的按钮,成功登录后,它会重定向回应用程序主页,并从JWT接收id_token和用户信息.

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

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

但是此方法返回与会话存储中存储为id_token(adal.idtoken)的相同令牌.它基本上通过串联密钥在会话存储中创建一个新项目,该密钥与id_token具有相同的值

adal.access_token.key + clientId = id_token

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

我还试图用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: 

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,response);
    // });
  }

  public get isAuthenticated() {
    return this.userInfo && this.accessToken;
  }
}
最佳答案
实际上,经过一些阅读后,发现将SPA连接到Azure AD需要OAuth 2.0 Implicit Grant流程. Microsoft documentation说:

In this scenario,when the user signs in,the JavaScript front end
uses 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 the
request as the bearer token when making calls to its Web API back end,
which is secured using the OWIN middleware.

因此,我需要将id_token本身发送到后端API,后端API又可以进行验证和使用.有关验证的更多信息,请参阅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 in
the token per your app’s requirements. The v2.0 endpoint uses JSON Web
Tokens (JWTs) and public key cryptography to sign tokens and verify
that they are valid.

You can choose to validate the id_token in client
code,but a common practice is to send the id_token to a backend
server and perform the validation there. Once you’ve validated the
signature of the id_token,there are a few claims you will be required
to verify.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


kindeditor4.x代码高亮功能默认使用的是prettify插件,prettify是Google提供的一款源代码语法高亮着色器,它提供一种简单的形式来着色HTML页面上的程序代码,实现方式如下: 首先在编辑器里面插入javascript代码: 确定后会在编辑器插入这样的代码: <pre
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代码高亮,因为SyntaxHighlighter的应用非常广泛,所以将kindeditor默认的prettify替换为SyntaxHighlighter代码高亮插件 上一篇“让kindeditor显示高亮代码”中已经
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小
JS怎么获取当前时间戳
JS如何判断对象是否为数组
JS怎么获取图片当前宽高
JS对象如何转为json格式字符串
JS怎么获取图片原始宽高
怎么在click事件中调用多个js函数
js如何往数组中添加新元素
js如何拆分字符串
JS怎么对数组内元素进行求和
JS如何判断屏幕大小
js怎么解析json数据
js如何实时获取浏览器窗口大小
原生JS实现别踩白块小游戏(五)
原生JS实现别踩白块小游戏(一)