将声明添加到令牌 Azure B2C

Add claims into token Azure B2C

在 Azure AD B2C 中发布令牌之前,可以通过哪些方法在令牌中包含自定义声明(例如用户订阅或角色列表),前提是声明存储在自己服务器上的某处(在 B2C 中不可用)? 目标是在令牌中声明,以避免在每次请求时额外往返存储。

对该主题的调查使我得出以下方法:

  1. 通过图形添加自定义属性 API,配置为包含在 JWT 中。属性值应与我们的数据存储保持同步。

  2. 自定义登录策略,如本文 https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-rest-api-step-custom 中所述,但如果我做对了,额外的第 6 步是以非限制方式公开可用的 API 的用户旅程(请求不受秘密保护,可能用于通过提供的 UserId 获取用户声明)?

  3. IdentityServer4 联合网关 http://docs.identityserver.io/en/release/topics/federation_gateway.html 允许在发布之前添加任何声明。

您概述的前两种机制是在 Azure AD B2C 颁发的令牌中包含自定义声明的最常见和推荐的方法:

  1. 添加一个custom attribute and include it in the JWT. You can enable the custom attribute via the B2C UI or via the Graph API. You'd need to build your own mechanism to keep the value of this attribute in B2C in sync with your external source via the Graph API.

  2. 你可以使用一个custom policy to add a step in your authentication flow to call a Rest API to obtain the claim and include it in the token。这个对 Rest API 的调用将由 Azure AD B2C 服务而不是用户的浏览器执行,因此它将是一个服务到服务的调用(相对于客户端到服务的调用),保留任何您用于 Rest API 安全身份验证的秘密(例如 Azure 函数代码)。