你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
重要
自 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 技术配置文件验证用户输入的挑战字符串。
更新内容定义的页面布局
对于各种页面布局,请使用以下页面布局版本:
页面布局 | 页面布局版本范围 |
---|---|
Selfasserted | >=2.1.33 |
Unifiedssp | >=2.1.21 |
多重 | >=1.2.19 |
示例:
在 TrustFrameworkBase.XML 文件中的元素下 ContentDefinitions
,找到 ID=“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 功能标志
若要在注册、登录或 MFA 时强制实施 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 显示控件。