我正在try 设置MSAL库以登录到Azure AD.使用APP_INIT中的重定向流,我能够登录并获得令牌.这是我使用的配置:

export function MSALInstanceFactory(): IPublicClientApplication {
  return new PublicClientApplication({
    auth: {
      clientId: '4015ef49-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
      authority: 'https://login.microsoftonline.com/2716828a-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
      protocolMode: ProtocolMode.OIDC,
      redirectUri: '/',
      postLogoutRedirectUri: '/'
    },
    cache: {
      cacheLocation: BrowserCacheLocation.LocalStorage,
      storeAuthStateInCookie: false
    },
    system: {
      // ... just logging
    }
  });
}

export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
  const protectedResourceMap = new Map<string, Array<string>>();
  protectedResourceMap.set('/', ['api://15c38238-xxxx-xxxx-xxxx-xxxxxxxxxxxx/api.test']);

  return {
    interactionType: InteractionType.Redirect,
    protectedResourceMap
  };
}

一切正常,当用户导航到页面时,他们立即被重定向到Azure登录屏幕,经过身份验证,导航回来,可以看到连接到user/getInfo端点的持有者令牌.

RAQABAAIAAAD--DLA3WO7QrddgJg7WeWrjQWKU1S2_FUBT2YAGgf8warZb_WeNfmM_d77BrbdQ8YxZ1rRq6xlREbnz1C5GRF1MikYUBKMs4wCUFbKju3AniERix1K1ZISi3RNgQjyS4rILQ8JhYfYL5adMWzfr1UjKheKZkdG8aZRHfRxi7DZua3WLYW5sAdBRWYttjQXWIJeFmRZbLDHUhhdYSRSxHGRli4tXQkcJtRtJOcWRY3EmQOAARRqcLJr8R3-EgRsmM3HNdGwbfBqRsjnNE6uass_t3YIzMib_CsfZAHGWnReutIl-x83a_wjWAWff8o3NlA65LXwaCI08n-Yeo8ji8-1fnS0iw1zEUh-nLsfdDCRWs0aF-zkuyoZSq52r28u2ujRz4HddWScQsZJJ6sUJCWScWGgLwXKGagEcyCHcIjZ-0-c5yIzyqRlcWRry5ZaqCTndU3GDhRmqDki5F1qQkY1HExqHKWxJDRH87YZ4brXrIdqLt1d8R2ufrqbWRQHMQHNqMCU8hNbS80mfEsnClf4OLNIQqW0A3LKyKIecuUnK5YCxtIhb8R8hUW4-M17hnG-5GmfRZOGODhbTqsYos1cIAA

但是,正如你所看到的,它是一根不透明的绳子,我不知道它包含什么.当我判断ID令牌时,我看到AUD属性的值是配置中提供的clientId:

aud: '4015ef49-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

然后,当我判断本地存储时,我可以找到一个包含提供的Web API资源的密钥:

target: "api://15c38238-xxxx-xxxx-xxxx-xxxxxxxxxxxx/api.test"

Be Guy告诉我,持有者是不正确的,它应该是我们可以解析的标准JWT.我不知道这是对是错. 那么我们都对AUD有疑问,它不是应该等于Web API clientId吗?是像这样吗?

aud: '15c38238-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

目前,我们的REST API抛出了401,我不知道这是因为FE端的一些错误配置,还是BE的问题?我的配置正确吗?

推荐答案

所以我们找到了答案,在FE和BE这两个方面都发现了问题.至于Angular 代码,我更改了两件事.

  1. 已删除OIDC协议.我认为这是使用OAuth方法时的必备条件,但事实证明并非如此.
export function MSALInstanceFactory(): IPublicClientApplication {
  return new PublicClientApplication({
    auth: {
      clientId: '4015ef49-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
      authority: 'https://login.microsoftonline.com/2716828a-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
      //protocolMode: ProtocolMode.OIDC, // After removing, Acces token arrives in standard JWT format
      redirectUri: '/',
      postLogoutRedirectUri: '/'
    },
    // ...
  });
}
  1. 将作用域直接添加到登录重定向调用.
const request: RedirectRequest = {
  scopes: [api://15c38238-xxxx-xxxx-xxxx-xxxxxxxxxxxx/api.test]
  // Add required 3rd party Web API scope to the redirect call
  // Works with popup login too.
};

return msal.loginRedirect(request).pipe(map(() => true));

这就解决了问题.

Angular相关问答推荐

从Angular 14更新到15个多边形.ts问题

如何在init为FALSE时以Angular 初始化主拇指擦除器-Swiper 11.0.6Angular 17 SSR

MAT表的内联文本编辑-未刷新编辑图标

如何重新显示ngbAlert(Bootstrap widgest for Angular)消息后再次驳回它

Angular 17:在MouseOver事件上 Select 子组件的正确ElementRef

无法匹配任何路由.URL段:';英雄';

角路径S异步旋转变压器仍可观察到

列表和映射的SCSS语法

Angular 组件存储,为什么我的效果可以';在http错误后不会被触发?

根据环境变量动态加载Angular templateUrl

Router.navigate() 在功能拦截器内不起作用

URL 更改但组件未在 Angular 导航中呈现

从 Angular 应用程序中删除散列标签后无法从 Apache 访问 API

Angular 14 类型表单 - 数字控件的初始值

Angular:try 在点击次数后禁用按钮

Subject.complete() 是否取消订阅所有听众?

如何在 Angular 4 中使用 Material Design 图标?

Angular 7 测试:NullInjectorError:No provider for ActivatedRoute

无法绑定到matDatepicker,因为它不是 input的已知属性

在Angular2的 Select 列表中设置最初 Select 的项目