You need to enable JavaScript to run this app. 如何通过Graph将Azure AD B2C自定义用户属性添加到用户流令牌声明
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Graph将Azure AD B2C自定义用户属性添加到用户流令牌声明

通过Graph为Azure AD B2C用户流添加自定义属性令牌声明的实现方案

该方案完全对齐Azure门户手动操作的效果,无需修改B2C自定义策略配置,全程可通过Graph API/SDK完成配置闭环。

前置校验准备

  • 确认已通过Graph创建好B2C自定义用户属性:该属性的标识符格式为custom_<自定义属性名>(对应identityUserFlowAttribute资源对象的ID),区别于目录层面的extension_<b2c扩展应用客户端ID>_<属性名>格式的扩展字段
  • 配置Graph调用权限:授予应用/委托权限IdentityUserFlow.ReadWrite.All,完成管理员同意授权
  • 确认目标用户流的ID:即门户中用户流列表展示的完整名称,例如B2C_1_signup_signin

优先方案:Graph SDK 实现示例

C# SDK 实现

// 基于已完成认证的GraphServiceClient实例直接调用,无需重复编写认证逻辑
var graphClient = new GraphServiceClient(credential, ["https://graph.microsoft.com/.default"]);

// 替换为实际的配置值
const string targetUserFlowId = "B2C_1_your_signin_flow";
const string customUserAttrId = "custom_userMobileTag";

// 构造声明绑定配置,完全等价于门户勾选添加应用声明的操作
var newClaimAssignment = new IdentityUserFlowAttributeAssignment
{
    UserAttribute = new IdentityUserFlowAttribute
    {
        Id = customUserAttrId
    },
    DisplayName = "User Mobile Tag",
    IsOptional = true,
    RequiresVerification = false,
    ShouldPersistRequired = true
};

// 提交配置绑定到目标用户流
await graphClient.Identity.UserFlows[targetUserFlowId]
    .UserAttributeAssignments
    .PostAsync(newClaimAssignment);

Python SDK 实现

from azure.identity import ClientSecretCredential
from msgraph import GraphServiceClient

# 初始化认证客户端
credential = ClientSecretCredential(
    tenant_id="你的B2C租户ID",
    client_id="Graph调用应用的客户端ID",
    client_secret="Graph调用应用的密钥"
)
graph_client = GraphServiceClient(credential, scopes=["https://graph.microsoft.com/.default"])

# 替换为实际配置
target_flow_id = "B2C_1_your_signin_flow"
custom_attr_id = "custom_userMobileTag"

# 构造请求体
claim_payload = {
    "userAttribute": {
        "id": custom_attr_id
    },
    "displayName": "User Mobile Tag",
    "isOptional": True,
    "requiresVerification": False,
    "shouldPersistRequired": True
}

# 提交绑定配置
await graph_client.identity.user_flows.by_identity_user_flow_id(target_flow_id).user_attribute_assignments.post(claim_payload)

纯Graph API 调用示例

如果不使用SDK,可直接通过REST请求完成配置:

  • 请求端点:POST https://graph.microsoft.com/v1.0/identity/userFlows/{目标用户流ID}/userAttributeAssignments
  • 请求头:携带有效访问令牌,格式为Authorization: Bearer <你的Graph访问令牌>
  • 请求体:
{
    "userAttribute": {
        "id": "custom_userMobileTag"
    },
    "displayName": "User Mobile Tag",
    "isOptional": true,
    "requiresVerification": false,
    "shouldPersistRequired": true
}

常见踩坑说明

  • 配置中传入的属性ID必须是identityUserFlowAttribute资源的ID(带custom_前缀),传入带extension_前缀的目录扩展属性ID会触发属性不存在的报错
  • 配置提交后1-2分钟内会自动同步到用户流运行时,无需手动重启用户流,登录拿到的令牌中会自动携带该自定义声明
  • 该接口仅适用于B2C内置预生成用户流,自定义策略的声明配置需要修改策略XML文件,无法通过该接口调整
  • 若出现重复添加同一声明的报错,可先调用GET /identity/userFlows/{用户流ID}/userAttributeAssignments查询现有已绑定的声明列表,跳过已存在的项

内容的提问来源于stack exchange,提问作者freak_geek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.06 07:24:03