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

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

注意

此功能處於公開預覽狀態。

重要

從 2021 年 5 月開始,GitHub 宣佈變更會影響您的 Azure AD B2C 自定義原則同盟。 由於變更,請將元數據新增 <Item Key="BearerTokenTransmissionMethod">AuthorizationHeader</Item> 至您的 GitHub 技術配置檔。 如需詳細資訊,請參閱 透過查詢參數取代 API 驗證。

必要條件

建立 GitHub OAuth 應用程式

若要在 Azure Active Directory B2C (Azure AD B2C) 中啟用 GitHub 帳戶登入,您必須在 GitHub 開發人員入口網站中建立應用程式。 如需詳細資訊,請參閱 建立 OAuth 應用程式。 如果您還沒有 GitHub 帳戶,您可以在 註冊 https://www.github.com/

  1. 使用您的 GitHub 認證登入 GitHub 開發人員
  2. 選取 [OAuth 應用程式 ],然後選取 [ 新增 OAuth 應用程式]。
  3. 輸入應用程式名稱和首頁 URL
  4. 針對 [ 授權回呼 URL],輸入 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-tenant-name您的租使用者名稱取代 your-domain-name 。 輸入租用戶名稱時,即使租使用者是以 Azure AD B2C 中的大寫字母定義,也請使用所有小寫字母。
  5. 按一下 [Register application](註冊應用程式)。
  6. 複製用戶端識別碼客戶端密碼的值。 您必須同時將識別提供者新增至租使用者。

將 GitHub 設定為識別提供者

  1. 以 Azure AD B2C 租使用者的全域管理員身分登入 Azure 入口網站
  2. 如果您有多個租使用者的存取權,請選取頂端功能表中的 設定 圖示,從 [目錄 + 訂用帳戶] 功能表切換至您的 Azure AD B2C 租使用者。
  3. 選擇 Azure 入口網站 左上角的 [所有服務],搜尋並選取 [Azure AD B2C]。
  4. 選取 [識別提供者],然後選取 [GitHub[預覽]。
  5. 輸入名稱。 例如, GitHub
  6. 針對 [ 用戶端識別符],輸入您稍早建立之 GitHub 應用程式的用戶端識別碼。
  7. 針對 [ 客戶端密碼],輸入您記錄的 [用戶端密碼]。
  8. 選取 [儲存]。

將 GitHub 識別提供者新增至使用者流程

此時,GitHub 識別提供者已設定,但尚未在任何登入頁面中提供。 若要將 GitHub 識別提供者新增至使用者流程:

  1. 在您的 Azure AD B2C 租使用者中,選取 [ 使用者流程]。
  2. 按兩下您要新增 GitHub 識別提供者的使用者流程。
  3. 在 [ 社交識別提供者] 底下,選取 [GitHub]。
  4. 選取 [儲存]。
  5. 若要測試您的原則,請選取 [ 執行使用者流程]。
  6. 針對 [ 應用程式],選取您先前註冊的名為 testapp1 的Web應用程式。 回覆 URL 應該會顯示 https://jwt.ms
  7. 選取 [ 執行使用者流程 ] 按鈕。
  8. 從 [註冊或登入] 頁面中,選取 [GitHub ] 以使用 GitHub 帳戶登入。

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

建立原則金鑰

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

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

將 GitHub 設定為識別提供者

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

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

  1. 開啟 TrustFrameworkExtensions.xml

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

  3. 新增 ClaimsProvider,如下所示:

    <ClaimsProvider>
      <Domain>github.com</Domain>
      <DisplayName>GitHub</DisplayName>
      <TechnicalProfiles>
        <TechnicalProfile Id="GitHub-OAuth2">
          <DisplayName>GitHub</DisplayName>
          <Protocol Name="OAuth2" />
          <Metadata>
            <Item Key="ProviderName">github.com</Item>
            <Item Key="authorization_endpoint">https://github.com/login/oauth/authorize</Item>
            <Item Key="AccessTokenEndpoint">https://github.com/login/oauth/access_token</Item>
            <Item Key="ClaimsEndpoint">https://api.github.com/user</Item>
            <Item Key="HttpBinding">GET</Item>
            <Item Key="scope">read:user user:email</Item>
            <Item Key="UsePolicyInRedirectUri">0</Item>
            <Item Key="BearerTokenTransmissionMethod">AuthorizationHeader</Item>  
            <Item Key="UserAgentForClaimsExchange">CPIM-Basic/{tenant}/{policy}</Item>
            <!-- Update the Client ID below to the Application ID -->
            <Item Key="client_id">Your GitHub application ID</Item>
          </Metadata>
          <CryptographicKeys>
            <Key Id="client_secret" StorageReferenceId="B2C_1A_GitHubSecret"/>
          </CryptographicKeys>
          <OutputClaims>
            <OutputClaim ClaimTypeReferenceId="displayName" PartnerClaimType="name" />
            <OutputClaim ClaimTypeReferenceId="email" PartnerClaimType="email" />
            <OutputClaim ClaimTypeReferenceId="numericUserId" PartnerClaimType="id" />
            <OutputClaim ClaimTypeReferenceId="issuerUserId" />
            <OutputClaim ClaimTypeReferenceId="identityProvider" DefaultValue="github.com" AlwaysUseDefaultValue="true" />
            <OutputClaim ClaimTypeReferenceId="authenticationSource" DefaultValue="socialIdpAuthentication" AlwaysUseDefaultValue="true" />
          </OutputClaims>
          <OutputClaimsTransformations>
            <OutputClaimsTransformation ReferenceId="CreateIssuerUserId" />
            <OutputClaimsTransformation ReferenceId="CreateRandomUPNUserName"/>
            <OutputClaimsTransformation ReferenceId="CreateUserPrincipalName"/>
            <OutputClaimsTransformation ReferenceId="CreateAlternativeSecurityId"/>
            <OutputClaimsTransformation ReferenceId="CreateSubjectClaimFromAlternativeSecurityId"/>
          </OutputClaimsTransformations>
          <UseTechnicalProfileForSessionManagement ReferenceId="SM-SocialLogin" />
        </TechnicalProfile>
      </TechnicalProfiles>
    </ClaimsProvider>
    
  4. 將client_id設定為應用程式註冊中的應用程式識別碼。

  5. 儲存檔案。

新增宣告轉換

GitHub 技術配置檔需要 將 CreateIssuerUserId 宣告轉換新增至 ClaimsTransformations 清單。 如果您沒有 在檔案中定義的 ClaimsTransformations 元素,請新增父 XML 元素,如下所示。 宣告轉換也需要定義名為 numericUserId 的新宣告類型。

  1. 搜尋 BuildingBlocks 元素。 如果專案不存在,請加以新增。
  2. 找出 ClaimsSchema 元素。 如果專案不存在,請加以新增。
  3. 將 numericUserId 宣告新增至 ClaimsSchema 元素。
  4. 找出 ClaimsTransformations 元素。 如果專案不存在,請加以新增。
  5. 將 CreateIssuerUserId 宣告轉換新增至 ClaimsTransformations 元素。
<BuildingBlocks>
  <ClaimsSchema>
    <ClaimType Id="numericUserId">
      <DisplayName>Numeric user Identifier</DisplayName>
      <DataType>long</DataType>
    </ClaimType>
  </ClaimsSchema>
  <ClaimsTransformations>
    <ClaimsTransformation Id="CreateIssuerUserId" TransformationMethod="ConvertNumberToStringClaim">
      <InputClaims>
        <InputClaim ClaimTypeReferenceId="numericUserId" TransformationClaimType="inputClaim" />
      </InputClaims>
      <OutputClaims>
        <OutputClaim ClaimTypeReferenceId="issuerUserId" TransformationClaimType="outputClaim" />
      </OutputClaims>
    </ClaimsTransformation>
  </ClaimsTransformations>
</BuildingBlocks>

新增使用者旅程圖

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

  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="GitHubExchange" />
  </ClaimsProviderSelections>
  ...
</OrchestrationStep>

<OrchestrationStep Order="2" Type="ClaimsExchange">
  ...
  <ClaimsExchanges>
    <ClaimsExchange Id="GitHubExchange" TechnicalProfileReferenceId="GitHub-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. 從 [註冊或登入] 頁面中,選取 [GitHub ] 以使用 GitHub 帳戶登入。

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

下一步