首页 > 解决方案 > Azure B2C中如何使用Microsoft Graph查询用户权限来源

问题描述

我正在使用 Graph 在 Azure B2C 中查询用户配置文件。我可以查询用户,但我没有看到 Source 字段来确定权限的来源。这是什么领域?

在此处输入图像描述

我目前正在使用 Microsoft.Graph.Beta NuGet 包的 .28 预览版。 在此处输入图像描述

这就是我在身份下的调试器中看到的: 在此处输入图像描述

如果那是 Google 帐户还是 Azure AD 帐户,我将如何区分?

标签: azureazure-active-directoryazure-ad-b2c

解决方案


我通过创建自定义属性解决了这个问题,然后在自定义策略中根据注册方法设置自定义属性(请参阅最后的替代解决方案)。

如何定义自定义属性并将它们与 MS Graph API 和自定义策略一起使用在此处进行了很好的解释。最困难的部分可能是让自定义策略正确。我在 TrustFrameworkExtensions.xml 中做了所有事情。首先定义一个“extension_authoritySource”ClaimType:

<ClaimType Id="extension_AuthoritySource">
  <DisplayName>AuthoritySource</DisplayName>
  <DataType>string</DataType>
</ClaimType>

然后在我添加了一个 OutputClaim ,它将这个自定义属性设置为 facebook,但是只有在 PersistedClaim如下所示<TechnicalProfile Id="Facebook-OAUTH">时才会持久化:UserWriteUsingAlternativeSecurityId

<OutputClaim ClaimTypeReferenceId="extension_AuthoritySource" DefaultValue="Facebook"/>

为了保留自定义属性,我将以下内容添加到 ClaimsProviders:

<ClaimsProvider>
    <DisplayName>Azure Active Directory</DisplayName>
    <TechnicalProfiles>
      <TechnicalProfile Id="AAD-Common">
        <Metadata>
          <Item Key="ClientId">[b2c-extensions-app application ID]</Item>
          <Item Key="ApplicationObjectId">[b2c-extensions-app application ObjectId]</Item>
        </Metadata>
      </TechnicalProfile>
    
    <!-- Write data during a local account sign-up flow. -->
    <TechnicalProfile Id="AAD-UserWriteUsingLogonEmail">
      <PersistedClaims>
        <PersistedClaim ClaimTypeReferenceId="extension_AuthoritySource" DefaultValue="local"/>
      </PersistedClaims>
    </TechnicalProfile>

      <TechnicalProfile Id="AAD-UserWriteUsingAlternativeSecurityId">
        <PersistedClaims>
          <PersistedClaim ClaimTypeReferenceId="extension_AuthoritySource" DefaultValue="social"/>
        </PersistedClaims>
      </TechnicalProfile>
  </TechnicalProfiles>
</ClaimsProvider>

请注意,上述电子邮件注册将始终设置为“本地”,而UserWriteUsingAlternativeSecurityId将其设置为“社交”,但会被 facebook 的输出声明覆盖。

我在这里的想法是,UserWriteUsingLogonEmail它只用于电子邮件注册,而UserWriteUsingAlternativeSecurityId可能被多个联合登录使用,尽管目前我只使用 facebook。

没有自定义属性的替代方案

或者,如果您没有使用自定义策略或由于其他原因无法使用上述方法,您可以使用MS Graph API并查看包含登录类型的“身份”数组。因此,对于给定的用户 GET:https://graph.microsoft.com/v1.0/users/[Users objectID Guid]?$select=identities

在此数组中,您可以找到本地注册:

{
  "signInType": "emailAddress",
  "issuer": "[yourdomain].onmicrosoft.com",
  "issuerAssignedId": "[email]"
}

对于脸书:

{
  "signInType": "federated",
  "issuer": "facebook.com",
  "issuerAssignedId": "[number]"
}

每个用户在身份数组中还有一个“userPrincipalName”项,因此您必须有一些逻辑来循环遍历数组,并且只查找您想要支持的 signInType。另一个更喜欢使用自定义属性并自己设置权限来源的原因。


推荐阅读