這很重要
自 2025 年 5 月 1 日起,Azure AD B2C 將不再可供新客戶購買。 在我們的常見問題中深入瞭解。
開始之前,請使用此頁面頂端的 [選擇原則類型] 選取器,選擇您要設定的原則類型。 Azure Active Directory B2C 提供兩種方法來定義使用者如何與您的應用程式互動:透過預先定義的使用者流程,或透過完全可設定的自訂原則。 此文章中所需的步驟隨各方法而異。
Azure Active Directory B2C (Azure AD B2C) 可讓您啟用 CAPTCHA,以防止對取用者面向應用程式的自動化攻擊。 Azure AD B2C 的 CAPTCHA 同時支援音訊和視覺 CAPTCHA 挑戰。 您可以在本機帳戶的註冊和登入流程中啟用這項安全性功能。 CAPTCHA 不適用於社交識別提供者的登入。
備註
這項功能處於公開預覽狀態
先決條件
- 建立使用者流程,讓使用者能夠註冊並登入您的應用程式。
- 註冊 Web 應用程式。
- 完成開始使用 Active Directory B2C 中的自定義原則中的步驟。 本教學課程會引導您更新自定義原則檔案,以使用您的 Azure AD B2C 租用戶設定。
- 註冊 Web 應用程式。
啟用 CAPTCHA
登入 Azure 入口網站。
如果您有多個租用戶的存取權,請使用頂端功能表中的 [設定] 圖示,從 [目錄 + 訂用帳戶] 功能表切換至您的 Azure AD B2C 租用戶。
在左側功能表中,選取 [Azure AD B2C]。 或者,選取 [所有服務 ],然後搜尋並選取 [Azure AD B2C]。
選取 [使用者流程]。
選取您想要啟用 CAPTCHA 的使用者流程。 例如, B2C_1_signinsignup。
選取 屬性。
在 CAPTCHA (預覽)下,選取要為其啟用 CAPTCHA 的流程,例如 [啟用 CAPTCHA - 註冊]。
選取 [儲存]。
測試使用者流程
使用 測試使用者流程 中的步驟來測試並確認已針對您選擇的流程啟用 CAPTCHA。 您應該被提示根據您選擇的 CAPTCHA 類型,視覺或音訊,輸入所看到或聽到的字元。
若要在自定義原則中啟用 CAPTCHA,您需要更新現有的自定義原則檔案。 如果您沒有任何現有的自定義原則檔案, 請下載 .zip 檔案 ,或從 https://github.com/Azure-Samples/active-directory-b2c-custom-policy-starterpack複製存放庫。 在本文中,我們會更新 /Display Controls Starterpack/LocalAccounts/ 資料夾中的 XML 檔案。
宣告索賠
您需要更多宣告,才能在自定義原則中啟用 CAPTCHA:
在 VS Code 中,開啟 TrustFrameworkBase.XML 檔案。
在區
ClaimsSchema段中,使用下列程序代碼宣告宣告:<!--<ClaimsSchema>--> ... <ClaimType Id="inputSolution"> <DataType>string</DataType> </ClaimType> <ClaimType Id="solved"> <DataType>boolean</DataType> </ClaimType> <ClaimType Id="reason"> <DataType>string</DataType> </ClaimType> <ClaimType Id="azureregion"> <DataType>string</DataType> </ClaimType> <ClaimType Id="challengeId"> <DisplayName>The ID of the generated captcha</DisplayName> <DataType>string</DataType> <UserHelpText>Captcha challenge identifier</UserHelpText> <UserInputType>Paragraph</UserInputType> </ClaimType> <ClaimType Id="challengeType"> <DisplayName>Type of captcha (visual / audio)</DisplayName> <DataType>string</DataType> <UserHelpText>Captcha challenge type</UserHelpText> <UserInputType>Paragraph</UserInputType> </ClaimType> <ClaimType Id="challengeString"> <DisplayName>Captcha challenge code</DisplayName> <DataType>string</DataType> <UserHelpText>Captcha challenge code</UserHelpText> <UserInputType>Paragraph</UserInputType> </ClaimType> <ClaimType Id="captchaEntered"> <DisplayName>Captcha entered by the user</DisplayName> <DataType>string</DataType> <UserHelpText>Enter the characters you see</UserHelpText> <UserInputType>TextBox</UserInputType> </ClaimType> <ClaimType Id="isCaptchaSolved"> <DisplayName>Flag indicating that the captcha was successfully solved</DisplayName> <DataType>boolean</DataType> </ClaimType> <ClaimType Id="mfaCaptchaEnabled"> <DisplayName>flag used to control captcha enabled in MFA</DisplayName> <DataType>string</DataType> </ClaimType> <ClaimType Id="signupCaptchaEnabled"> <DisplayName>flag used to control captcha enabled during signup</DisplayName> <DataType>string</DataType> </ClaimType> <ClaimType Id="signinCaptchaEnabled"> <DisplayName>flag used to control captcha enabled during signin</DisplayName> <DataType>string</DataType> </ClaimType> ... <!--<ClaimsSchema>-->
設定顯示控制件
若要啟用自定義原則的 CAPTCHA,您可以使用 CAPTCHA 顯示控件。 CAPTCHA 顯示控件會產生並轉譯 CAPTCHA 影像。
在 TrustFrameworkBase.XML 檔案中,找出 DisplayControls 元素,然後將下列顯示控件新增為子元素。 如果您還沒有 DisplayControls 元素,請新增一個。
<!--<DisplayControls>-->
...
<DisplayControl Id="captchaControlChallengeCode" UserInterfaceControlType="CaptchaControl" DisplayName="Help us beat the bots">
<InputClaims>
<InputClaim ClaimTypeReferenceId="challengeType" />
<InputClaim ClaimTypeReferenceId="challengeId" />
</InputClaims>
<DisplayClaims>
<DisplayClaim ClaimTypeReferenceId="challengeType" ControlClaimType="ChallengeType" />
<DisplayClaim ClaimTypeReferenceId="challengeId" ControlClaimType="ChallengeId" />
<DisplayClaim ClaimTypeReferenceId="challengeString" ControlClaimType="ChallengeString" />
<DisplayClaim ClaimTypeReferenceId="captchaEntered" ControlClaimType="CaptchaEntered" />
</DisplayClaims>
<Actions>
<Action Id="GetChallenge">
<ValidationClaimsExchange>
<ValidationClaimsExchangeTechnicalProfile
TechnicalProfileReferenceId="HIP-GetChallenge" />
</ValidationClaimsExchange>
</Action>
<Action Id="VerifyChallenge">
<ValidationClaimsExchange>
<ValidationClaimsExchangeTechnicalProfile
TechnicalProfileReferenceId="HIP-VerifyChallenge" />
</ValidationClaimsExchange>
</Action>
</Actions>
</DisplayControl>
...
<!--</DisplayControls>-->
配置 CAPTCHA 技術設定檔
Azure AD B2C CAPTCHA 技術配置文件 會驗證 CAPTCHA 挑戰。 此技術配置檔可以產生 CAPTCHA 驗證碼,或根據您的設定進行驗證。
在 TrustFrameworkBase.XML 檔案中,找出 ClaimsProviders 元素,並使用下列程式代碼新增宣告提供者:
<!--<ClaimsProvider>-->
...
<ClaimsProvider>
<DisplayName>HIPChallenge</DisplayName>
<TechnicalProfiles>
<TechnicalProfile Id="HIP-GetChallenge">
<DisplayName>GetChallenge</DisplayName>
<Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.CaptchaProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Metadata>
<Item Key="Operation">GetChallenge</Item>
<Item Key="Brand">HIP</Item>
</Metadata>
<InputClaims>
<InputClaim ClaimTypeReferenceId="challengeType" />
</InputClaims>
<DisplayClaims>
<DisplayClaim ClaimTypeReferenceId="challengeString" />
</DisplayClaims>
<OutputClaims>
<OutputClaim ClaimTypeReferenceId="challengeId" />
<OutputClaim ClaimTypeReferenceId="challengeString" PartnerClaimType="ChallengeString" />
<OutputClaim ClaimTypeReferenceId="azureregion" />
</OutputClaims>
</TechnicalProfile>
<TechnicalProfile Id="HIP-VerifyChallenge">
<DisplayName>Verify Code</DisplayName>
<Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.CaptchaProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Metadata>
<Item Key="Brand">HIP</Item>
<Item Key="Operation">VerifyChallenge</Item>
</Metadata>
<InputClaims>
<InputClaim ClaimTypeReferenceId="challengeType" DefaultValue="Visual" />
<InputClaim ClaimTypeReferenceId="challengeId" />
<InputClaim ClaimTypeReferenceId="captchaEntered" PartnerClaimType="inputSolution" Required="true" />
<InputClaim ClaimTypeReferenceId="azureregion" />
</InputClaims>
<DisplayClaims>
<DisplayClaim ClaimTypeReferenceId="captchaEntered" />
</DisplayClaims>
<OutputClaims>
<OutputClaim ClaimTypeReferenceId="challengeId" />
<OutputClaim ClaimTypeReferenceId="isCaptchaSolved" PartnerClaimType="solved" />
<OutputClaim ClaimTypeReferenceId="reason" PartnerClaimType="reason" />
</OutputClaims>
</TechnicalProfile>
</TechnicalProfiles>
</ClaimsProvider>
...
<!--<ClaimsProviders>-->
您使用 GetChallenge 作業設定的 CAPTCHA 技術設定檔會產生並顯示 CAPTCHA 挑戰字串。 您使用 VerifyChallenge 設定的 CAPTCHA 技術設定檔會驗證使用者輸入的挑戰字串。
更新內容定義的版面配置
針對各種版面配置,請使用下列版面配置版本:
| 頁面配置 | 版面配置版本範圍 |
|---|---|
| 自我宣稱的 | >=2.1.33 |
| 統一 ssp | >=2.1.21 |
| 多因子 | >=1.2.19 |
範例︰
在 TrustFrameworkBase.XML 檔案的 ContentDefinitions 元素底下,找出 標識符=“api.localaccountsignup” 的內容定義,然後更新其 DataUri ,如下列程式代碼所示:
<!---<ContentDefinitions>-->
...
<ContentDefinition Id="api.localaccountsignup">
...
<!--Update this DataUri-->
<DataUri>urn:com:microsoft:aad:b2c:elements:contract:selfasserted:2.1.27</DataUri>
...
</ContentDefinition>
...
<!---</ContentDefinitions>-->
我們會將自我定義版面配置指定為 2.1.27。
設定技術設定檔和顯示控制件之後,您可以指定您想要啟用 CAPTCHA 的流程。
啟用 CAPTCHA 以進行註冊或登入流程
若要啟用註冊或登入流程的 CAPTCHA,請使用下列步驟:
檢查您的註冊登入使用者旅程圖,例如 SignUpOrSignIn,以識別顯示您註冊或登入體驗的自我判斷技術配置檔。
在技術配置檔中,例如 LocalAccountSignUpWithLogonEmail,新增元數據密鑰和顯示宣告專案,如下列程式代碼所示:
<TechnicalProfile Id="LocalAccountSignUpWithLogonEmail">
...
<Metadata>
...
<!--Add this metadata entry. Set value to true to activate CAPTCHA-->
<Item Key="setting.enableCaptchaChallenge">true</Item>
...
</Metadata>
...
<DisplayClaims>
...
<!--Add this display claim, which is a reference to the captcha display control-->
<DisplayClaim DisplayControlReferenceId="captchaControlChallengeCode" />
...
</DisplayClaims>
...
</TechnicalProfile>
顯示宣告項目會參考您稍早設定的顯示控件。
在 MFA 流程中啟用 CAPTCHA
若要在 MFA 流程中啟用 CAPTCHA,您必須在兩個技術配置檔中進行更新,也就是在自我判斷技術配置檔中,以及在 手機要素技術配置檔中:
檢查您的註冊登入使用者旅程,例如 SignUpOrSignIn,以識別負責註冊或登入流程的自我聲明技術檔案和電話因素技術檔案。
在這兩個技術側寫中,新增一個元數據鍵和一個展示宣告條目,如下列程式碼所示:
<TechnicalProfile Id="PhoneFactor-InputOrVerify">
...
<Metadata>
...
<!--Add this metadata entry. Value set to true-->
<Item Key="setting.enableCaptchaChallenge">true</Item>
...
</Metadata>
...
<DisplayClaims>
...
<!--Add this display claim-->
<DisplayClaim DisplayControlReferenceId="captchaControlChallengeCode" />
...
</DisplayClaims>
...
</TechnicalProfile>
啟用 CAPTCHA 功能旗標
若要在註冊、登入或多因素驗證過程中強制使用 CAPTCHA,您必須新增一個技術設定檔以啟用每個情境的功能標誌,然後在使用者旅程中呼叫該技術設定檔。
- 在 TrustFrameworkBase.XML 檔案中,找出
ClaimsProviders元素,並使用下列程式代碼新增宣告提供者:
<!--<ClaimsProvider>-->
...
<ClaimsProvider>
<DisplayName>Set Feature Flags</DisplayName>
<TechnicalProfiles>
<TechnicalProfile Id="SetFeatureDefaultValue">
<DisplayName>Set Feature Flags</DisplayName>
<Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.ClaimsTransformationProtocolProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<OutputClaims>
<OutputClaim ClaimTypeReferenceId="signupCaptchaEnabled" DefaultValue="true" />
<OutputClaim ClaimTypeReferenceId="signinCaptchaEnabled" DefaultValue="true" />
<OutputClaim ClaimTypeReferenceId="mfaCaptchaEnabled" DefaultValue="true" />
</OutputClaims>
</TechnicalProfile>
</TechnicalProfiles>
</ClaimsProvider>
...
<!--<ClaimsProviders>-->
根據 CAPTCHA 案例設定
DefaultValue為 true 或 false將功能旗標技術配置檔新增至使用者旅程圖,然後更新其餘協調流程步驟的順序。
<!--<UserJourneys>-->
...
<UserJourney Id="SignUpOrSignIn">
<OrchestrationSteps>
<!--Add this orchestration step-->
<OrchestrationStep Order="1" Type="ClaimsExchange">
<ClaimsExchanges>
<ClaimsExchange Id="SetFeatureDefaultValue" TechnicalProfileReferenceId="SetFeatureDefaultValue" />
</ClaimsExchanges>
</OrchestrationStep>
...
<!--<UserJourneys>-->
上傳自定義原則檔案
使用 上傳原則 中的步驟來上傳您的自定義原則檔案。
測試自定義原則
使用 測試自定義原則 中的步驟來測試並確認已針對您選擇的流程啟用 CAPTCHA。 您應該被提示根據您選擇的 CAPTCHA 類型,視覺或音訊,輸入所看到或聽到的字元。
備註
- 您無法將 CAPTCHA 新增至僅限註冊使用者流程中的 MFA 步驟。
- 在 MFA 流程中,當您選擇的 MFA 方法是短信與電話、僅限短信或僅限電話時,CAPTCHA 適用。
相關內容
- 瞭解如何 定義 CAPTCHA 技術配置檔。
- 瞭解如何 設定 CAPTCHA 顯示控制件。