這很重要
自 2025 年 5 月 1 日起,Azure AD B2C 將不再可供新客戶購買。 在我們的常見問題中深入瞭解。
開始之前,請使用此頁面頂端的 [選擇原則類型] 選取器,選擇您要設定的原則類型。 Azure Active Directory B2C 提供兩種方法來定義使用者如何與您的應用程式互動:透過預先定義的使用者流程,或透過完全可設定的自訂原則。 此文章中所需的步驟隨各方法而異。
此功能僅適用於自訂原則。 如需設定步驟,請在前述選取器中選取 [自訂原則]。
先決條件
- 完成開始使用 Active Directory B2C 中的自定義原則中的步驟。 本教學課程會引導您更新自定義原則檔案,以使用您的 Azure AD B2C 租用戶設定。
- 如果您尚未註冊 Web 應用程式,請使用 註冊 Web 應用程式中的步驟來註冊一個 Web 應用程式。
建立 ID.me 應用程式
若要在 Azure Active Directory B2C (Azure AD B2C) 中啟用具有 ID.me 帳戶的使用者登入,您必須在 適用於 API 和 SDK 的開發人員資源 ID.me 中建立應用程式。 如需詳細資訊,請參閱 OAuth 整合指南。 如果您還沒有 ID.me 開發人員帳戶,您可以在 註冊 https://developers.id.me/registration/new。
- 使用您的 ID.me 帳戶認證登入 適用於 API 和 SDK 的 ID.me 開發人員資源 。
- 選取 [檢視我的應用程式],然後選取 [ 繼續]。
- 選取 [新建]
- 輸入 名稱 和 顯示名稱。
- 在 [ 重新導向 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-tenant-name
替換為您的租戶名稱,並將your-domain-name
替換為您的自訂網域。
- 按一下繼續。
- 複製 用戶端識別碼 和 客戶端密碼的值。 您必須同時將識別提供者新增至租使用者。
建立原則金鑰
您必須儲存您先前在 Azure AD B2C 租用戶中記錄的客戶端密碼。
- 登入 Azure 入口網站。
- 如果您有多個租用戶的存取權,請使用頂端功能表中的 [設定] 圖示,從 [目錄 + 訂用帳戶] 功能表切換至您的 Azure AD B2C 租用戶。
- 選擇 Azure 入口網站左上角 的 [所有服務 ],然後搜尋並選取 [Azure AD B2C]。
- 在 [概觀] 頁面上,選取 [ 身分識別體驗架構]。
- 選取 [原則密鑰 ],然後選取 [ 新增]。
- 針對 [選項],選擇
Manual
。 - 輸入政策金鑰的名稱。 例如:
IdMeSecret
。 前置詞B2C_1A_
會自動新增至金鑰的名稱。 - 在 [ 秘密] 中,輸入您先前記錄的客戶端密碼。
- 針對 [ 金鑰使用方式],選取
Signature
。 - 點擊 建立。
將 ID.me 設定為識別提供者
若要讓使用者們能夠使用 ID.me 帳戶登入,您需要將該帳戶定義為 Azure AD B2C 可以透過端點進行通訊的宣告提供者。 端點提供一組宣告,供 Azure AD B2C 用來驗證特定使用者已驗證。
您可以將 ID.me 帳戶定義為宣告提供者,方法是將它新增至原則擴充檔案中的 ClaimsProviders 元素。
開啟 TrustFrameworkExtensions.xml。
尋找 ClaimsProviders 元素。 如果不存在,請在根元素底下新增它。
新增 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>
將 client_id 設定為應用程式註冊中的應用程式識別碼。
儲存檔案。
新增主張轉換
接下來,您需要進行「聲明轉換」來建立 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>
新增使用者旅程圖
此時,身份提供者已設定,但尚未在任何登入頁面中可用。 如果您沒有自己的自定義使用者旅程圖,請建立現有範本使用者旅程圖的重複項目,否則請繼續進行下一個步驟。
- 從入門套件開啟 TrustFrameworkBase.xml 檔案。
- 尋找並複製包含 之
Id="SignUpOrSignIn"
元素的整個內容。 - 開啟 TrustFrameworkExtensions.xml 並尋找 UserJourneys 元素。 如果元素不存在,新增一個。
- 貼上您複製的 UserJourney 元素作為 UserJourneys 元素子元素的整個內容。
- 重新命名使用者旅程的識別碼。 例如:
Id="CustomSignUpSignIn"
。
將識別提供者新增至使用者旅程圖
現在您已擁有使用者旅程圖,請將新的識別提供者新增至使用者旅程圖。 您必須先新增登入按鈕,然後將按鈕連結至動作。 動作項目是您稍早建立的技術配置檔。
尋找使用者旅程中包含
Type="CombinedSignInAndSignUp"
或Type="ClaimsProviderSelection"
的協調流程步驟元素。 通常是第一個編排流程步驟。 ClaimsProviderSelections 元素包含使用者可以登入的識別提供者清單。 元素的順序會控制向用戶呈現的登入按鈕順序。 新增 ClaimsProviderSelection XML 元素。 將 TargetClaimsExchangeId 的值設定為易記名稱。在下一個協調流程步驟中,新增 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>
上傳自定義原則
- 登入 Azure 入口網站。
- 在入口網站工具列中選取 [目錄 + 訂 用帳戶] 圖示,然後選取包含 Azure AD B2C 租使用者的目錄。
- 在 Azure 入口網站中,搜尋並選取 [Azure AD B2C]。
- 在 [ 原則] 底下,選取 [ 身分識別體驗架構]。
- 選取 [上傳自定義原則],然後上傳您變更的兩個原則檔案,順序如下:擴充原則,例如
TrustFrameworkExtensions.xml
,然後是信賴憑證者原則,例如SignUpSignIn.xml
。
測試您的自定義原則
- 選取您的受信方政策,例如
B2C_1A_signup_signin
。 - 針對 [應用程式],選取您 先前註冊的 Web 應用程式。
Reply URL 應顯示
https://jwt.ms
。 - 選取 [ 立即執行] 按鈕。
- 從 [註冊或登入] 頁面中,選取 [ID.me ] 以使用 ID.me 帳戶登入。
如果登入程式成功,您的瀏覽器會重新導向至 https://jwt.ms
,以顯示 Azure AD B2C 所傳回令牌的內容。