在 Azure Active Directory B2C 中將識別提供者存取令牌傳遞至您的應用程式

開始之前,請使用 [選擇原則類型 選取器] 來選擇您要設定的原則類型。 Azure Active Directory B2C 提供兩種方法來定義使用者如何與您的應用程式互動:透過預先 定義的使用者流程 ,或透過完全可設定 的自定義原則。 本文中每個方法所需的步驟都不同。

Azure Active Directory B2C (Azure AD B2C) 中的使用者流程可讓使用者利用身分識別提供者註冊或登入應用程式。 當旅程開始時,Azure AD B2C 會 從識別提供者接收存取令牌 。 Azure AD B2C 使用該令牌來擷取使用者的相關信息。 您可以在使用者流程中啟用宣告,以將令牌傳遞至您在 Azure AD B2C 中註冊的應用程式。

Azure AD B2C 支援傳遞 OAuth 2.0 識別提供者的存取令牌,其中包括 FacebookGoogle。 針對所有其他識別提供者,宣告會傳回空白。

Azure AD B2C 支援傳遞 OAuth 2.0OpenID 連線 識別提供者的存取令牌。 針對所有其他識別提供者,宣告會傳回空白。 如需詳細資訊,請參閱身分識別提供者同盟 Live 示範

下圖顯示識別提供者令牌如何傳回您的應用程式:

Identity provider pass through flow

必要條件

啟用宣告

  1. 以 Azure AD B2C 租使用者的全域管理員身分登入 Azure 入口網站

  2. 如果您有多個租使用者的存取權,請選取頂端功能表中的 [設定] 圖示,從 [目錄 + 訂用帳戶] 功能表切換至您的 Azure AD B2C 租使用者。

  3. 選擇 Azure 入口網站 左上角的 [所有服務],搜尋並選取 [Azure AD B2C]。

  4. 選取 [使用者流程][原則],然後選取您的使用者流程。 例如, B2C_1_signupsignin1

  5. 選取 [ 應用程式宣告]。

  6. 啟用識別 提供者存取令牌 宣告。

    Enable the Identity Provider Access Token claim

  7. 按兩下 [ 儲存 ] 以儲存使用者流程。

測試使用者流程

在 Azure AD B2C 中測試您的應用程式時,讓傳回 Azure AD B2C 令牌來 https://jwt.ms 檢閱其中宣告會很有用。

  1. 在使用者流程的 [概觀] 頁面上,選取 [ 執行使用者流程]。

  2. 針對 [ 應用程式],選取您先前註冊的應用程式。 若要檢視下列範例中的權杖, 回覆 URL 應該會顯示 https://jwt.ms

  3. 按兩下 [ 執行使用者流程],然後使用您的帳戶認證登入。 您應該會在idp_access_token宣告中看到識別提供者的存取令牌。

    您應該會看到類似下列範例的內容:

    Decoded token in jwt.ms with idp_access_token block highlighted

新增宣告專案

  1. 開啟 TrustframeworkExtensions.xml 檔案,並將具有 標識符identityProviderAccessToken的下列 ClaimType 元素新增至 ClaimsSchema 元素:

    <BuildingBlocks>
      <ClaimsSchema>
        <ClaimType Id="identityProviderAccessToken">
          <DisplayName>Identity Provider Access Token</DisplayName>
          <DataType>string</DataType>
          <AdminHelpText>Stores the access token of the identity provider.</AdminHelpText>
        </ClaimType>
        ...
      </ClaimsSchema>
    </BuildingBlocks>
    
  2. 針對 您想要存取令牌的每個 OAuth 2.0 識別提供者,將 OutputClaim 元素新增至 TechnicalProfile 元素。 下列範例顯示新增至 Facebook 技術設定檔的 元素:

    <ClaimsProvider>
      <DisplayName>Facebook</DisplayName>
      <TechnicalProfiles>
        <TechnicalProfile Id="Facebook-OAUTH">
          <OutputClaims>
            <OutputClaim ClaimTypeReferenceId="identityProviderAccessToken" PartnerClaimType="{oauth2:access_token}" />
          </OutputClaims>
          ...
        </TechnicalProfile>
      </TechnicalProfiles>
    </ClaimsProvider>
    
  3. 儲存 TrustframeworkExtensions.xml 檔案。

  4. 開啟信賴憑證者原則檔案,例如 SignUpOrSignIn.xml,並將 OutputClaim 元素新增TechnicalProfile

    <RelyingParty>
      <DefaultUserJourney ReferenceId="SignUpOrSignIn" />
      <TechnicalProfile Id="PolicyProfile">
        <OutputClaims>
          <OutputClaim ClaimTypeReferenceId="identityProviderAccessToken" PartnerClaimType="idp_access_token"/>
        </OutputClaims>
        ...
      </TechnicalProfile>
    </RelyingParty>
    
  5. 儲存原則檔案。

測試您的原則

在 Azure AD B2C 中測試您的應用程式時,讓傳回的 https://jwt.ms Azure AD B2C 令牌能夠檢閱其中宣告會很有用。

上傳檔案

  1. 登入 Azure 入口網站
  2. 如果您有多個租使用者的存取權,請選取頂端功能表中的 設定 圖示,從 [目錄 + 訂用帳戶] 功能表切換至您的 Azure AD B2C 租使用者。
  3. 選擇 Azure 入口網站 左上角的 [所有服務],然後搜尋並選取 [Azure AD B2C]。
  4. 選取 [ 身分識別體驗架構]。
  5. 在 [自定義原則] 頁面上,按兩下 [ 上傳原則]。
  6. 選取 [覆寫原則是否存在],然後搜尋並選取 TrustframeworkExtensions.xml 檔案。
  7. 選取上傳
  8. 針對信賴憑證者檔案重複步驟 5 到 7,例如 SignUpOrSignIn.xml

執行原則

  1. 開啟您變更的原則。 例如, B2C_1A_signup_signin

  2. 針對 [ 應用程式],選取您先前註冊的應用程式。 若要檢視下列範例中的權杖, 回覆 URL 應該會顯示 https://jwt.ms

  3. 選取 [ 立即執行]。

    您應該會看到類似下列範例的內容:

    Decoded token in jwt.ms with idp_access_token block highlighted

傳遞 IDP 重新整理權杖 (選擇性)

識別提供者傳回的存取令牌在短時間內有效。 某些識別提供者也會發出重新整理令牌以及存取令牌。 然後,用戶端應用程式可以視需要交換識別提供者的重新整理令牌,以取得新的存取令牌。

Azure AD B2C 自定義原則支持傳遞 OAuth 2.0 識別提供者的重新整理令牌,其中包括 FacebookGoogleGitHub

若要傳遞識別提供者的重新整理令牌,請遵循下列步驟:

  1. 開啟 TrustframeworkExtensions.xml 檔案,並將具有標識符identityProviderRefreshToken的下列 ClaimType 元素新增至 ClaimsSchema 元素。

    <ClaimType Id="identityProviderRefreshToken">
        <DisplayName>Identity provider refresh token</DisplayName>
        <DataType>string</DataType>
    </ClaimType>
    
  2. 針對 您想要重新整理令牌的每個 OAuth 2.0 識別提供者,將 OutputClaim 元素新增至 TechnicalProfile 元素。 下列範例顯示新增至 Facebook 技術設定檔的 元素:

    <ClaimsProvider>
      <DisplayName>Facebook</DisplayName>
      <TechnicalProfiles>
        <TechnicalProfile Id="Facebook-OAUTH">
          <OutputClaims>
            <OutputClaim ClaimTypeReferenceId="identityProviderRefreshToken" PartnerClaimType="{oauth2:refresh_token}" />
          </OutputClaims>
          ...
        </TechnicalProfile>
      </TechnicalProfiles>
    </ClaimsProvider>
    
  3. 某些識別提供者要求您包含身分識別提供者技術配置檔的元數據或範圍。

    • 針對 Google 身分識別提供者,新增兩個宣告類型和 access_typeprompt。 然後將下列輸入宣告新增至識別提供者的技術配置檔:

      <InputClaims>
          <InputClaim ClaimTypeReferenceId="access_type" PartnerClaimType="access_type" DefaultValue="offline" AlwaysUseDefaultValue="true" />
      
          <!-- The refresh_token is return only on the first authorization for a given user. Subsequent authorization request doesn't return the refresh_token.
              To fix this issue we add the prompt=consent query string parameter to the authorization request-->
          <InputClaim ClaimTypeReferenceId="prompt" PartnerClaimType="prompt" DefaultValue="consent" AlwaysUseDefaultValue="true" />
      </InputClaims>
      
    • 其他識別提供者可能有不同的方法來發出重新整理令牌。 請遵循識別提供者的物件,並將必要的元素新增至識別提供者的技術配置檔。

  4. 儲存您在 TrustframeworkExtensions.xml 檔案中所做的變更。

  5. 開啟信賴憑證者原則檔案,例如 SignUpOrSignIn.xml,並將 OutputClaim 元素新增TechnicalProfile

    <RelyingParty>
      <DefaultUserJourney ReferenceId="SignUpOrSignIn" />
      <TechnicalProfile Id="PolicyProfile">
        <OutputClaims>
          <OutputClaim ClaimTypeReferenceId="identityProviderRefreshToken" PartnerClaimType="idp_refresh_token"/>
        </OutputClaims>
        ...
      </TechnicalProfile>
    </RelyingParty>
    
  6. 儲存您在原則信賴憑證者原則檔案中所做的變更。

  7. 上傳 TrustframeworkExtensions.xml 檔案,然後上傳信賴憑證者原則檔案。

  8. 測試您的原則

下一步

在 Azure AD B2C 令牌概觀中深入瞭解。