微软作为个人账户的 OAuth2 提供者不颁发 JWT 访问令牌

Microsoft as OAuth2 provider for personal accounts does not issue JWT access tokens

微软似乎不会为个人账户发放JWT访问令牌。

在 Azure AD 中,我创建了类型为 'Personal Microsoft accounts only' 的应用程序注册。
我的 SPA 使用该应用程序注册,我可以对其进行身份验证。
验证我的 SPA 后收到一个 ID 令牌和一个访问令牌。
但是访问令牌看起来不像 JsonWebToken。

AFAIK 访问令牌始终以字符 'eyJ' 开头,但 Azure AD 发出的令牌以 'EwC' 开头,当我在 jwt.ms 上调试访问令牌时,控制台告诉我'Invalid token specified: Unexpected token'.

但只有我选择账户类型'Personal Microsoft accounts only'才会出现这种情况。
如果我使用 'Accounts in this organizational directory only'.

类型,它会起作用

当我将无效令牌发送到我的 REST Api 时,我收到一个 401 响应 header WWW-Authenticate: Bearer error="invalid_token".
api 配置为接受 JWT Bearer 令牌并使用相同的 Azure AD 应用程序注册。

使用颁发的令牌调用 OAuth userinfo enpoint 工作正常。

值得一提的是,我通过个人 Microsoft 帐户使用 Azure。

是否可以通过个人 Microsoft 帐户获取 JWT 访问令牌?

请检查以下情况是否属于您的解决方法

1) JSON Web Tokens 由三部分组成,由点 (.) 分隔,分别是:Header.Payload.signature .

(图片from

在其中一种情况下 this 令牌具有格式

access_token=EwC...  &authentication_token=eyJ… 

即;访问令牌以 EwC 开头,而身份验证以 eyJ

开头

所以在你的情况下(SPA),看起来它使用 'id_token' 代替 'access_token'

id_token 是 JSON Web Token,用于识别经过身份验证的用户,例如对于单点登录。 access_token 必须用于证明对受保护资源的访问权限,例如对于 OpenId Connect 中的用户信息端点。
因此,请尝试使用 id_token 进行调试,它可能充当访问令牌。

ID tokens | Microsoft Docs

有时令牌过期可能是提供无效令牌身份验证的原因,因为令牌在过期后可能无效。

因此,检查令牌生命周期并创建一个新的身份验证请求。

scenarios-spa

Also see scenario-spa-app-registration which says

If your application signs in users, select ID tokens.

If your application also needs to call a protected web API, select Access tokens. For more information about these token types, see ID tokens and Access tokens.

2)

Once the app gets registered , the app communicates with the Microsoft identity platform by sending requests to the endpoint:

https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize

https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token

Where the {tenant} can take one of different values:

common Allows users with both personal Microsoft accounts and work/school accounts from Azure AD to sign into the application.

consumers 仅允许拥有个人 Microsoft 帐户 (MSA) 的用户登录应用程序。

即;在启动 class 或在 SPA

中说 authconfig 文件

代码示例:/quickstart-v2-javascript-auth-code

           Authority:”https://login.microsoftonline.com/consumers/......”, 

参考文献:

  1. active-directory-v2-protocols#endpoints
  2. c-sharp-asp-net-core-bearer-error-invalid-token

阅读友好手册 (RTFM) 已经回答了我的问题。
我必须在 Azure 门户中创建两个应用程序注册。
一个用于我的 SPA,一个用于我的 REST API,然后授予我 SPA 访问 REST API.

的权限

这里描述得很好:
https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-configure-app-access-web-apis

发送初始身份验证请求时,范围必须包括授予我的 REST 的权限 API。
响应将包含为 REST API.

颁发的 JWT 访问令牌