【发布时间】:2021-12-09 08:35:49
【问题描述】:
我目前正在开发一种在 JWT 令牌中添加自定义声明的解决方案。
设置如下:
- Azure B2C 有一个包含多个声明提供程序的自定义策略。
- Azure AD 的声明提供程序配置了标准声明和我的自定义声明。
- 用于调用 REST API 的声明提供程序。
- REST API 收集配置为从 Graph API 收集自定义属性,它配置为使用“电子邮件”的输入声明来查找与用户登录相关的数据,以及使用我的名称的“输出”声明自定义声明。
- 在用户
SignUpSignIn旅程中添加了一个步骤,用于在发布 JWT 令牌之前调用 REST API。
用户旅程成功运行,我可以重定向到 Azure AD 进行身份验证。我可以看到 REST API 是从 Azure 中的日志触发的,并且我获得了我的 JWT 令牌,但我没有看到包含任何声明。
当我检查 REST API 日志时,我可以看到函数运行成功,如果我在 Azure 函数中手动运行测试选项,我还可以看到成功响应,这是一个 JSON 数据数组。因此,这表明我的 REST API 正在以应有的方式工作。我还通过 PowerShell 和 Web 浏览器成功地对其进行了测试,并且在正文中得到了正确的响应。
我怀疑问题出在返回值并将其插入到自定义声明中以便它出现在我的 JWT 令牌中的过程中。
仅供参考:我正在使用的自定义声明是返回用户在 AzureAD 中是 memberOf 的安全组。
<ClaimsProvider>
<DisplayName>REST API</DisplayName>
<TechnicalProfiles>
<TechnicalProfile Id="GetUserGroups">
<DisplayName>Retrieves security groups assigned to the user</DisplayName>
<Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.RestfulProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Metadata>
<Item Key="ServiceUrl">https://contoso.azurewebsites.net/api/aadgroups?code=123456789QWERTYUIOP123456789QWERTYUIOP==</Item>
<Item Key="AuthenticationType">None</Item>
<Item Key="SendClaimsIn">Body</Item>
<Item Key="AllowInsecureAuthInProduction">true</Item>
</Metadata>
<InputClaims>
<InputClaim ClaimTypeReferenceId="email" />
</InputClaims>
<OutputClaims>
<OutputClaim ClaimTypeReferenceId="restapigroups" />
</OutputClaims>
<UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
</TechnicalProfile>
</TechnicalProfiles>
</ClaimsProvider>
<ClaimType Id="restapigroups">
<DisplayName>Group memberships</DisplayName>
<DataType>stringCollection</DataType>
<UserHelpText>This is read only for the user</UserHelpText>
<UserInputType>Readonly</UserInputType>
</ClaimType>
<OrchestrationStep Order="11" Type="ClaimsExchange">
<ClaimsExchanges>
<ClaimsExchange Id="AADUserGroups" TechnicalProfileReferenceId="GetUserGroups" />
</ClaimsExchanges>
</OrchestrationStep>
【问题讨论】:
-
另外需要说明的是,我曾尝试遵循如下详述的声明转换方法,但这并不成功并且可能不适用:docs.microsoft.com/en-us/azure/active-directory-b2c/…
标签: jwt azure-active-directory azure-functions azure-ad-b2c azure-ad-graph-api