You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将所有组ID纳入Azure B2C的Group声明

更优处理Azure AD B2C用户组ID集合的方案

当前通过逐个声明变量再添加到字符串集合的方式扩展性很差——用户组数量变化时就得修改策略新增变量和转换逻辑。这里有个更简洁的方案,直接将Graph API返回的组ID数组映射到集合类型声明,无需逐个处理:

步骤1:声明集合类型的ClaimType

先在TrustFrameworkExtensions.xml的<ClaimsSchema>里定义一个stringCollection类型的声明,用来存储所有组ID:

<ClaimType Id="groups">
  <DisplayName>用户所属组ID集合</DisplayName>
  <DataType>stringCollection</DataType>
</ClaimType>

步骤2:在Graph API调用的TechnicalProfile中直接映射数组

修改调用Graph API的TechnicalProfile,把接口返回的value数组里的id字段,直接映射到上面定义的groups集合声明:

<TechnicalProfile Id="AAD-GraphGetGroups">
  <DisplayName>从Graph API获取用户组</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://graph.microsoft.com/v1.0/users/{objectId}/transitiveMemberOf?$select=id</Item>
    <Item Key="AuthenticationType">Bearer</Item>
    <Item Key="SendClaimsIn">Url</Item>
    <Item Key="UseClaimAsBearerToken">accessToken</Item>
  </Metadata>
  <InputClaims>
    <InputClaim ClaimTypeReferenceId="objectId" />
    <InputClaim ClaimTypeReferenceId="accessToken" />
  </InputClaims>
  <OutputClaims>
    <!-- 直接将返回的value数组中的id字段映射到stringCollection类型的groups -->
    <OutputClaim ClaimTypeReferenceId="groups" PartnerClaimType="value.id" />
  </OutputClaims>
  <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
</TechnicalProfile>

步骤3:在令牌中输出集合声明

最后在RelyingParty的<OutputClaims>里添加这个声明,就能让组ID集合以["grp1_id","grp2_id"]的格式出现在令牌中:

<OutputClaim ClaimTypeReferenceId="groups" PartnerClaimType="groups" />

方案优势

  • 无需预定义固定数量的组变量,自动适配用户所属的任意数量组
  • 省去多个AddItemToStringCollection类型的ClaimsTransformation,策略逻辑更简洁
  • 减少后续维护成本,用户组数量变化时无需修改策略

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 22:52:12