Return REST API 响应作为对 Azure B2C 的输出声明
Return REST API response as an output claim to Azure B2C
我目前正在开发一种在 JWT 令牌中添加自定义声明的解决方案。
设置如下:
- Azure B2C 有一个包含多个声明提供商的自定义策略。
- Azure AD 的声明提供程序配置了标准声明和我的自定义声明。
- 用于调用 REST 的声明提供程序 API。
- REST API 收集被配置为从图形 API 收集自定义属性,它被配置为使用 'email' 的输入声明来查找与用户日志记录相关的数据在和一个 'output' 声明中,其中包含我的自定义声明的名称。
- 在发布 JWT 令牌之前,用户
SignUpSignIn
旅程中添加了一个步骤来调用 REST API。
用户旅程成功,我可以重定向到 Azure AD 进行身份验证。我可以看到 REST API 从 Azure 中的日志中被触发,并且我获得了我的 JWT 令牌,但我没有看到包含任何声明。
当我检查 REST API 日志时,我可以看到函数 运行 成功,如果我手动 运行 我的 Azure 函数中的测试选项,我也可以看到成功响应,这是一个 JSON 数据数组。所以这表明我的 REST API 正在正常工作。我还通过 PowerShell 和 Web 浏览器成功测试了它,并在正文中正确地得到了我的响应。
我怀疑问题出在 returning 值并将其插入自定义声明以便它出现在我的 JWT 令牌中的过程中。
仅供参考:我正在使用的自定义声明是 return 用户是 AzureAD 中成员的安全组。
<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>
您必须配置 RelyingParty 部分以将声明输出到令牌中。看这个例子:https://docs.microsoft.com/en-us/azure/active-directory-b2c/custom-policy-rest-api-claims-exchange#add-the-claim
我目前正在开发一种在 JWT 令牌中添加自定义声明的解决方案。
设置如下:
- Azure B2C 有一个包含多个声明提供商的自定义策略。
- Azure AD 的声明提供程序配置了标准声明和我的自定义声明。
- 用于调用 REST 的声明提供程序 API。
- REST API 收集被配置为从图形 API 收集自定义属性,它被配置为使用 'email' 的输入声明来查找与用户日志记录相关的数据在和一个 'output' 声明中,其中包含我的自定义声明的名称。
- 在发布 JWT 令牌之前,用户
SignUpSignIn
旅程中添加了一个步骤来调用 REST API。
用户旅程成功,我可以重定向到 Azure AD 进行身份验证。我可以看到 REST API 从 Azure 中的日志中被触发,并且我获得了我的 JWT 令牌,但我没有看到包含任何声明。
当我检查 REST API 日志时,我可以看到函数 运行 成功,如果我手动 运行 我的 Azure 函数中的测试选项,我也可以看到成功响应,这是一个 JSON 数据数组。所以这表明我的 REST API 正在正常工作。我还通过 PowerShell 和 Web 浏览器成功测试了它,并在正文中正确地得到了我的响应。
我怀疑问题出在 returning 值并将其插入自定义声明以便它出现在我的 JWT 令牌中的过程中。
仅供参考:我正在使用的自定义声明是 return 用户是 AzureAD 中成员的安全组。
<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>
您必须配置 RelyingParty 部分以将声明输出到令牌中。看这个例子:https://docs.microsoft.com/en-us/azure/active-directory-b2c/custom-policy-rest-api-claims-exchange#add-the-claim