使用用户流在 Azure B2C 中添加自定义声明

问题描述 投票:0回答:1

在 Azure B2C 中使用外部 IDP。是否可以在不使用自定义策略的情况下向 JWT 添加自定义声明?

在我的 IDP 中,我的 /connect/token 端点中有此代码:

这按预期工作,“名字”设置为 SSN。

identity.RemoveClaims(Claims.GivenName);
identity.AddClaim(Claims.GivenName, identity.Claims.First(x => x.Type == "SSN").Value);

但这不起作用:

identity.AddClaim("extension_9322349238409238_SSN", identity.Claims.First(x => x.Type == "SSN").Value);

如果我使用 API 连接器,我确实可以使用声明来丰富令牌,但我需要在实际访问 SSN 之前执行此操作。

如果没有自定义策略,这可能吗?文档对此并不是 100% 清楚。

https://learn.microsoft.com/en-us/azure/active-directory-b2c/user-flow-custom-attributes?pivots=b2c-user-flow

注意:我不想使用用户输入来收集声明,而这可以使用用户属性来实现。

azure .net-core azure-ad-b2c claims-based-identity idp
1个回答
0
投票

我在 Azure AD B2C 中创建了一个 自定义属性“SSN”

enter image description here

创建了 Azure AD B2C 用户流并选择 SSN 作为应用程序声明

enter image description here

对于 示例,将 Google 配置为身份提供商:

enter image description here

当我运行用户流程时,我选择使用 Google 登录:

enter image description here

用户登录后,将在 Azure AD B2C 租户中创建用户:

enter image description here

复制用户的对象 ID 并将自定义属性值分配给用户:

PATCH https://graph.microsoft.com/v1.0/users/UserID

{
"extension_Yourb2c-extensions-appAPPIDwithouthyphens_SSN": "ruk"
}

enter image description here

运行用户流程,将生成ID令牌和访问令牌。

当我解码访问令牌时,自定义声明SSN已成功显示:

enter image description here

否则,您可以直接选择给定名称作为应用程序声明,当您解码令牌时,它将显示为

"given_name": "ruk"

© www.soinside.com 2019 - 2024. All rights reserved.