分享方式:


使用 Azure Active Directory B2C 以 ID.me 帳戶設定註冊和登入

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

此功能僅適用於自定義原則。 針對安裝步驟,請在上述選取器中選取 [自定義原則 ]。

必要條件

建立 ID.me 應用程式

若要在 Azure Active Directory B2C (Azure AD B2C) 中啟用具有 ID.me 帳戶的使用者登入,您必須在適用於 API & SDK 的開發人員資源 ID.me 中建立應用程式。 如需詳細資訊,請參閱 OAuth 整合指南。 如果您還沒有 ID.me 開發人員帳戶,您可以在 註冊 https://developers.id.me/registration/new

  1. 使用 ID.me 帳戶認證登入 適用於 API & SDK 的 ID.me 開發人員資源。
  2. 選取 [ 檢視我的應用程式],然後選取 [ 繼續]。
  3. 選取 [新建]
    1. 輸入 [名稱] [顯示名稱]。
    2. 在 [ 重新導向 URI] 中,輸入 https://your-tenant-name.b2clogin.com/your-tenant-name.onmicrosoft.com/oauth2/authresp。 如果您使用 自訂網域,請輸入 https://your-domain-name/your-tenant-name.onmicrosoft.com/oauth2/authresp。 取代為您的租使用者名稱,並以your-domain-name您的自訂網域取代 your-tenant-name
  4. 按一下 [繼續]。
  5. 複製用戶端識別碼客戶端密碼的值。 您必須同時將識別提供者新增至租使用者。

建立原則金鑰

您必須儲存您先前在 Azure AD B2C 租用戶中記錄的客戶端密碼。

  1. 登入 Azure 入口網站
  2. 如果您有多個租使用者的存取權,請選取頂端功能表中的 [設定] 圖示,從 [目錄 + 訂用帳戶] 功能表切換至您的 Azure AD B2C 租使用者。
  3. 選擇 Azure 入口網站 左上角的 [所有服務],然後搜尋並選取 [Azure AD B2C]。
  4. 在 [概觀] 頁面上,選取 [ 身分識別體驗架構]。
  5. 選取 [ 原則密鑰 ],然後選取 [ 新增]。
  6. 針對 [ 選項],選擇 Manual
  7. 輸入 原則金鑰的 [名稱 ]。 例如: IdMeSecret 。 前置詞 B2C_1A_ 會自動新增至金鑰的名稱。
  8. 在 [秘密] 中,輸入您先前記錄的客戶端密碼。
  9. 針對 [ 金鑰使用方式],選取 Signature
  10. 按一下 [建立]。

將 ID.me 設定為識別提供者

若要讓使用者使用 ID.me 帳戶登入,您必須將帳戶定義為 Azure AD B2C 可以透過端點通訊的宣告提供者。 端點提供一組宣告,供 Azure AD B2C 用來驗證特定使用者已驗證。

您可以將 ID.me 帳戶定義為宣告提供者,方法是將它新增至 原則擴充檔案中的 ClaimsProviders 元素。

  1. 開啟 TrustFrameworkExtensions.xml

  2. 尋找 ClaimsProviders 元素。 如果不存在,請在根元素底下新增它。

  3. 新增 ClaimsProvider,如下所示:

    <ClaimsProvider>
      <Domain>id.me</Domain>
      <DisplayName>ID.me</DisplayName>
      <TechnicalProfiles>
        <TechnicalProfile Id="IdMe-OAuth2">
          <DisplayName>IdMe</DisplayName>
          <Protocol Name="OAuth2" />
          <Metadata>
            <Item Key="ProviderName">api.id.me</Item>
            <Item Key="authorization_endpoint">https://api.id.me/oauth/authorize</Item>
            <Item Key="AccessTokenEndpoint">https://api.id.me/oauth/token</Item>
            <Item Key="ClaimsEndpoint">https://api.id.me/api/public/v2/attributes.json</Item>
            <Item Key="HttpBinding">POST</Item>
            <Item Key="scope">openid alumni</Item>
            <Item Key="UsePolicyInRedirectUri">0</Item>
            <!-- Update the Client ID below to the Application ID -->
            <Item Key="client_id">Your ID.me application ID</Item>
          </Metadata>
          <CryptographicKeys>
            <Key Id="client_secret" StorageReferenceId="B2C_1A_IdMeSecret"/>
          </CryptographicKeys>
          <OutputClaims>
            <OutputClaim ClaimTypeReferenceId="issuerUserId" PartnerClaimType="uuid" />
            <OutputClaim ClaimTypeReferenceId="givenName" PartnerClaimType="fname" />
            <OutputClaim ClaimTypeReferenceId="surname" PartnerClaimType="lname" />
            <OutputClaim ClaimTypeReferenceId="displayName" PartnerClaimType="name" />
            <OutputClaim ClaimTypeReferenceId="email" PartnerClaimType="email" />
            <OutputClaim ClaimTypeReferenceId="identityProvider" DefaultValue="me.id.com" AlwaysUseDefaultValue="true" />
            <OutputClaim ClaimTypeReferenceId="authenticationSource" DefaultValue="socialIdpAuthentication" AlwaysUseDefaultValue="true" />
          </OutputClaims>
          <OutputClaimsTransformations>
            <OutputClaimsTransformation ReferenceId="CreateRandomUPNUserName" />
            <OutputClaimsTransformation ReferenceId="CreateUserPrincipalName" />
            <OutputClaimsTransformation ReferenceId="CreateAlternativeSecurityId" />
            <OutputClaimsTransformation ReferenceId="CreateDisplayNameFromFirstNameAndLastName" />
          </OutputClaimsTransformations>
          <UseTechnicalProfileForSessionManagement ReferenceId="SM-SocialLogin" />
        </TechnicalProfile>
      </TechnicalProfiles>
    </ClaimsProvider>
    
  4. 將client_id設定為應用程式註冊中的應用程式識別碼。

  5. 儲存檔案。

新增宣告轉換

接下來,您需要宣告轉換來建立 displayName 宣告。 將下列宣告轉換新增至 <ClaimsTransformations> 內的 <BuildingBlocks>專案。

 <ClaimsTransformations>
  <ClaimsTransformation Id="CreateDisplayNameFromFirstNameAndLastName" TransformationMethod="FormatStringMultipleClaims">
    <InputClaims>
      <InputClaim ClaimTypeReferenceId="givenName" TransformationClaimType="inputClaim1" />
      <InputClaim ClaimTypeReferenceId="surName" TransformationClaimType="inputClaim2" />
    </InputClaims>
    <InputParameters>
      <InputParameter Id="stringFormat" DataType="string" Value="{0} {1}" />
    </InputParameters>
    <OutputClaims>
      <OutputClaim ClaimTypeReferenceId="displayName" TransformationClaimType="outputClaim" />
    </OutputClaims>
   </ClaimsTransformation>
</ClaimsTransformations>

新增使用者旅程圖

此時,識別提供者已設定,但尚未在任何登入頁面中提供。 如果您沒有自己的自定義使用者旅程圖,請建立現有範本使用者旅程圖的重複項目,否則請繼續進行下一個步驟。

  1. 從入門套件開啟 TrustFrameworkBase.xml 檔案。
  2. 尋找並複製包含 Id="SignUpOrSignIn"之 UserJourney 元素的整個內容
  3. 開啟 TrustFrameworkExtensions.xml 並尋找 UserJourneys 元素。 如果專案不存在,請新增一個。
  4. 貼上您複製為 UserJourneys 元素子系之 UserJourney 元素整個內容。
  5. 重新命名使用者旅程圖的標識碼。 例如: Id="CustomSignUpSignIn"

將識別提供者新增至使用者旅程圖

現在您已擁有使用者旅程圖,請將新的識別提供者新增至使用者旅程圖。 您必須先新增登入按鈕,然後將按鈕連結至動作。 動作是您稍早建立的技術配置檔。

  1. 尋找在 Type="CombinedSignInAndSignUp"使用者旅程圖中包含 或 Type="ClaimsProviderSelection" 的協調流程步驟元素。 通常是第一個協調流程步驟。 ClaimsProviderSelections 元素包含使用者可以登入的識別提供者清單。 元素的順序會控制向用戶呈現的登入按鈕順序。 新增 ClaimsProviderSelection XML 元素。 將 TargetClaimsExchangeId 的值設定為易記名稱。

  2. 在下一個 協調流程步驟中,新增 ClaimsExchange 元素。 將標識符設定為目標宣告交換標識碼的值。將TechnicalProfileReferenceId的值更新為您稍早建立之技術配置檔的標識碼。

下列 XML 示範使用者旅程圖的前兩個協調流程步驟與識別提供者:

<OrchestrationStep Order="1" Type="CombinedSignInAndSignUp" ContentDefinitionReferenceId="api.signuporsignin">
  <ClaimsProviderSelections>
    ...
    <ClaimsProviderSelection TargetClaimsExchangeId="IdMeExchange" />
  </ClaimsProviderSelections>
  ...
</OrchestrationStep>

<OrchestrationStep Order="2" Type="ClaimsExchange">
  ...
  <ClaimsExchanges>
    <ClaimsExchange Id="IdMeExchange" TechnicalProfileReferenceId="IdMe-OAuth2" />
  </ClaimsExchanges>
</OrchestrationStep>

設定信賴憑證者原則

信賴憑證者原則,例如 SignUpSignIn.xml,會指定 Azure AD B2C 將執行的使用者旅程圖。 尋找信賴憑證者內的DefaultUserJourney元素。 更新 ReferenceId 以符合您新增識別提供者的使用者旅程圖標識碼。

在下列範例中 CustomSignUpSignIn ,針對使用者旅程圖, ReferenceId 會設定為 CustomSignUpSignIn

<RelyingParty>
  <DefaultUserJourney ReferenceId="CustomSignUpSignIn" />
  ...
</RelyingParty>

上傳自定義原則

  1. 登入 Azure 入口網站
  2. 在入口網站工具列中選取 [ 目錄 + 訂 用帳戶] 圖示,然後選取包含 Azure AD B2C 租使用者的目錄。
  3. 在 Azure 入口網站中,搜尋並選取 [Azure AD B2C]
  4. 在 [原則] 底下,選取 [身分識別體驗架構]。
  5. 選取 [ 上傳自定義原則],然後上傳您變更的兩個原則檔案,順序如下:擴充原則,例如 TrustFrameworkExtensions.xml,然後是信賴憑證者原則,例如 SignUpSignIn.xml

測試您的自定義原則

  1. 選取您的信賴憑證者原則,例如 B2C_1A_signup_signin
  2. 針對 [ 應用程式],選取您 先前註冊的 Web 應用程式。 回覆 URL 應該會顯示 https://jwt.ms
  3. 選取 [ 立即 執行] 按鈕。
  4. 從 [註冊或登入] 頁面中,選取 [ID.me 以 ID.me 帳戶登入。

如果登入程式成功,您的瀏覽器會重新導向至 https://jwt.ms,以顯示 Azure AD B2C 所傳回令牌的內容。