如何使用 Azure AD B2C 获取 Facebook 个人资料图片

Posted

技术标签:

【中文标题】如何使用 Azure AD B2C 获取 Facebook 个人资料图片【英文标题】:how to get Facebook profile picture using Azure AD B2C 【发布时间】:2018-06-10 09:08:27 【问题描述】:

我正在使用 MSAL.js,并且可以使用 Facebook 作为身份提供者在 Azure AD B2C 中成功登录/注册用户。问题是登录后我无法检索用户的头像。

Azure AD B2C 返回一个与用户的 Facebook id 没有关联的对象标识符。

【问题讨论】:

您是否使用自定义策略? 不,据我了解,要使用自定义政策,我需要拥有 ad b2c 高级帐户。 嗨@armache。自定义策略是定义任何 Azure AD B2C 租户行为的配置文件;他们不需要特殊的租户。下面,我发布了如何检索 Facebook 用户的图片字段,然后使用自定义策略在 ID 令牌中发出图片声明。 【参考方案1】:

使用自定义策略,您可以检索 Facebook 用户的图片字段,然后在 ID 令牌中发出图片声明,如下所示。

1:使用社交帐户策略之一完成 Azure Active Directory B2C: Get started with custom policies 步骤,例如 SocialAndLocalAccounts one。

2:在the extensions file 中声明“图片”声明:

<ClaimType Id="picture">
  <DisplayName>Picture</DisplayName>
  <DataType>string</DataType>
</ClaimType>

3:将“图片”字段添加到“ClaimsEndpoint”元数据项,并将“图片”输出声明添加到the extensions policy 中的“Facebook-OAUTH”技术配置文件:

<ClaimsProvider>
  <DisplayName>Facebook</DisplayName>
  <TechnicalProfiles>
    <TechnicalProfile Id="Facebook-OAUTH">
      <Metadata>
        <Item Key="client_id">facebook_clientid</Item>
        <Item Key="scope">email public_profile</Item>
        <Item Key="ClaimsEndpoint">https://graph.facebook.com/me?fields=id,first_name,last_name,name,email,picture</Item>
      </Metadata>
      <OutputClaims>
        <OutputClaim ClaimTypeReferenceId="picture" PartnerClaimType="picture" />
      </OutputClaims>
    </TechnicalProfile>
  </TechnicalProfiles>
</ClaimsProvider>

4:在the sign-up or sign-in relying party policy中发出“图片”声明:

<RelyingParty>
  <DefaultUserJourney ReferenceId="SignUpOrSignIn" />
  <TechnicalProfile Id="PolicyProfile">
    <DisplayName>PolicyProfile</DisplayName>
    <Protocol Name="OpenIdConnect" />
    <OutputClaims>
      <OutputClaim ClaimTypeReferenceId="displayName" />
      <OutputClaim ClaimTypeReferenceId="givenName" />
      <OutputClaim ClaimTypeReferenceId="surname" />
      <OutputClaim ClaimTypeReferenceId="email" />
      <OutputClaim ClaimTypeReferenceId="picture" />
      <OutputClaim ClaimTypeReferenceId="objectId" PartnerClaimType="sub"/>
      <OutputClaim ClaimTypeReferenceId="identityProvider" />
    </OutputClaims>
    <SubjectNamingInfo ClaimType="sub" />
  </TechnicalProfile>
</RelyingParty>

【讨论】:

步骤 2 中提到的 xml。我在基本文件中看到类似的东西,而不是扩展文件。我需要把它放在基本文件中吗? @armache 嗨@RaasMasood。您可以在基本文件或扩展文件中添加“图片”声明类型。 我是这个概念的新手,所以请帮助我。我添加了它,所以我应该期望图片在哪里?我有一个 Xamarin 测试应用程序,AcuireToken 调用给了我 User 对象。我应该期望图片在哪里? 图片应该是用户对象声明部分的属性?它会包含图片的 URL 吗?请帮忙 这对我不起作用,很遗憾这些实现都没有开箱即用的这一重要功能。

以上是关于如何使用 Azure AD B2C 获取 Facebook 个人资料图片的主要内容,如果未能解决你的问题,请参考以下文章

我应该如何获取 Azure AD B2C 用户的上次登录时间戳?

如何使用Azure AD B2C保护Spring Boot REST API?

如何使用 Azure AD B2C 保护 Blazor Wasm 应用访问的 Azure 函数?

在 Azure B2C 中获取刷新令牌,Azure AD App 是第三方 IDP

在 Postman 中为 Azure AD B2C 请求访问令牌

如何在 Azure AD B2C 中管理用户流之间的单个帐户/会话