你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
重要
自 2025 年 5 月 1 日起,Azure AD B2C 将不再可供新客户购买。 在我们的常见问题解答中了解详细信息。
注释
在 Azure Active Directory B2C 中,自定义策略 主要用于解决复杂场景。 对于大多数场景,我们建议您使用内置的用户流。 如果未这样做,请通过 Active Directory B2C 中的自定义策略入门来了解自定义策略初学者包。
的 Predicates 和 PredicateValidations 元素使您能够执行验证过程,以确保仅将格式正确的数据输入到 Azure Active Directory B2C (Azure AD B2C) 租户中。
下图显示了元素之间的关系:
谓词
Predicate 元素定义基本验证以检查声明类型的值,并返回 true
or false
。 验证是使用指定的 Method 元素和一组与方法相关的 Parameter 元素来完成的。 例如,谓词可以检查字符串声明值的长度是否在指定的最小和最大参数范围内,或者字符串声明值是否包含字符集。 如果检查失败, UserHelpText 元素会向用户提供错误消息。
UserHelpText 元素的值可以使用语言自定义进行本地化。
Predicates 元素必须直接出现在 BuildingBlocks 元素中的 ClaimsSchema 元素之后。
Predicates 元素包含以下元素:
元素 | 出现次数 | DESCRIPTION |
---|---|---|
谓词 | 1:n | 谓词列表。 |
Predicate 元素包含以下属性:
特征 | 必选 | DESCRIPTION |
---|---|---|
Id | 是的 | 用于谓词的标识符。 其他元素可以在策略中使用此标识符。 |
方法 | 是的 | 用于验证的方法类型。 可能的值: IsLengthRange、 MatchesRegex、 IncludesCharacters 或 IsDateRange。 |
HelpText | 否 | 检查失败时向用户显示错误消息。 此字符串可以使用语言自定义进行本地化 |
Predicate 元素包含以下元素:
元素 | 出现次数 | DESCRIPTION |
---|---|---|
UserHelpText | 0:1 | (已弃用)检查失败时向用户显示错误消息。 |
参数 | 1:1 | 字符串验证的方法类型的参数。 |
Parameters 元素包含以下元素:
元素 | 出现次数 | DESCRIPTION |
---|---|---|
参数 | 1:n | 字符串验证的方法类型的参数。 |
Parameter 元素包含以下属性:
元素 | 出现次数 | DESCRIPTION |
---|---|---|
Id | 1:1 | 参数的标识符。 |
谓词方法
IsLengthRange
IsLengthRange 方法检查字符串声明值的长度是否在指定的最小和最大参数范围内。 查看此谓词方法的 Live 演示 。 predicate 元素支持以下参数:
参数 | 必选 | DESCRIPTION |
---|---|---|
最大值 | 是的 | 可输入的最大字符数。 |
最小值 | 是的 | 必须输入的最小字符数。 |
以下示例显示了一个 IsLengthRange 方法,该方法包含参数 Minimum
并 Maximum
指定字符串的长度范围:
<Predicate Id="IsLengthBetween8And64" Method="IsLengthRange" HelpText="The password must be between 8 and 64 characters.">
<Parameters>
<Parameter Id="Minimum">8</Parameter>
<Parameter Id="Maximum">64</Parameter>
</Parameters>
</Predicate>
MatchesRegex (匹配正则表达式)
MatchesRegex 方法检查字符串声明值是否与正则表达式匹配。 查看此谓词方法的 Live 演示 。 predicate 元素支持以下参数:
参数 | 必选 | DESCRIPTION |
---|---|---|
RegularExpression | 是的 | 要匹配的正则表达式模式。 |
以下示例显示了一个 MatchesRegex
方法,该方法的参数 RegularExpression
指定了正则表达式:
<Predicate Id="PIN" Method="MatchesRegex" HelpText="The password must be numbers only.">
<Parameters>
<Parameter Id="RegularExpression">^[0-9]+$</Parameter>
</Parameters>
</Predicate>
包含角色
IncludesCharacters 方法检查字符串声明值是否包含字符集。 查看此谓词方法的 Live 演示 。 predicate 元素支持以下参数:
参数 | 必选 | DESCRIPTION |
---|---|---|
CharacterSet | 是的 | 可以输入的字符集。 例如, lowercase characters a-z 、 uppercase characters A-Z 、 digits 0-9 或符号列表,如 @#$%^&*\-_+=[]{}|\\:',?/~"();! . |
以下示例显示了一个 IncludesCharacters
方法,该方法的参数 CharacterSet
用于指定字符集:
<Predicate Id="Lowercase" Method="IncludesCharacters" HelpText="a lowercase letter">
<Parameters>
<Parameter Id="CharacterSet">a-z</Parameter>
</Parameters>
</Predicate>
IsDateRange
IsDateRange 方法检查日期声明值是否介于指定的最小和最大参数范围之间。 查看此谓词方法的 Live 演示 。 predicate 元素支持以下参数:
参数 | 必选 | DESCRIPTION |
---|---|---|
最大值 | 是的 | 可以输入的最大可能日期。 日期的格式遵循 yyyy-mm-dd 约定,或 Today . |
最小值 | 是的 | 可以输入的最小日期。 日期的格式遵循 yyyy-mm-dd 约定,或 Today . |
以下示例显示了一个 IsDateRange
带有参数 Minimum
的方法,该方法 Maximum
使用格式为 yyyy-mm-dd
和 指定 Today
日期范围。
<Predicate Id="DateRange" Method="IsDateRange" HelpText="The date must be between 1970-01-01 and today.">
<Parameters>
<Parameter Id="Minimum">1970-01-01</Parameter>
<Parameter Id="Maximum">Today</Parameter>
</Parameters>
</Predicate>
PredicateValidations
谓词定义验证以针对声明类型进行检查,而 PredicateValidations 则对一组谓词进行分组,以形成可应用于声明类型的用户输入验证。 每个 PredicateValidation 元素都包含一组 PredicateGroup 元素,这些元素包含一组指向 Predicate 的 PredicateReference 元素。 若要通过验证,声明的值应通过所有 PredicateGroup 及其 PredicateReference 元素集下的任何谓词的所有测试。
PredicateValidations 元素必须直接显示在 BuildingBlocks 元素中的 Predicates 元素之后。
<PredicateValidations>
<PredicateValidation Id="">
<PredicateGroups>
<PredicateGroup Id="">
<UserHelpText></UserHelpText>
<PredicateReferences MatchAtLeast="">
<PredicateReference Id="" />
...
</PredicateReferences>
</PredicateGroup>
...
</PredicateGroups>
</PredicateValidation>
...
</PredicateValidations>
PredicateValidations 元素包含以下元素:
元素 | 出现次数 | DESCRIPTION |
---|---|---|
PredicateValidation | 1:n | 谓词验证的列表。 |
PredicateValidation 元素包含以下属性:
特征 | 必选 | DESCRIPTION |
---|---|---|
Id | 是的 | 用于谓词验证的标识符。 ClaimType 元素可以在策略中使用此标识符。 |
PredicateValidation 元素包含以下元素:
元素 | 出现次数 | DESCRIPTION |
---|---|---|
谓词组 | 1:n | 谓词组的列表。 |
PredicateGroups 元素包含以下元素:
元素 | 出现次数 | DESCRIPTION |
---|---|---|
PredicateGroup | 1:n | 谓词列表。 |
PredicateGroup 元素包含以下属性:
特征 | 必选 | DESCRIPTION |
---|---|---|
Id | 是的 | 用于谓词组的标识符。 |
PredicateGroup 元素包含以下元素:
元素 | 出现次数 | DESCRIPTION |
---|---|---|
UserHelpText | 0:1 | 谓词的描述,有助于用户了解他们应该键入的值。 |
PredicateReferences | 1:n | 谓词引用列表。 |
PredicateReferences 元素包含以下属性:
特征 | 必选 | DESCRIPTION |
---|---|---|
MatchAtLeast (匹配最小) | 否 | 指定该值必须至少与该数量的谓词定义匹配,才能接受输入。 如果未指定,则该值必须与所有谓词定义匹配。 |
PredicateReferences 元素包含以下元素:
元素 | 出现次数 | DESCRIPTION |
---|---|---|
PredicateReference (谓词引用) | 1:n | 对谓词的引用。 |
PredicateReference 元素包含以下属性:
特征 | 必选 | DESCRIPTION |
---|---|---|
Id | 是的 | 用于谓词验证的标识符。 |
配置密码复杂性
使用 Predicates 和 PredicateValidationsInput ,您可以控制用户在创建帐户时提供的密码的复杂性要求。 默认情况下,Azure AD B2C 使用强密码。 Azure AD B2C 还支持配置选项来控制客户可以使用的密码的复杂性。 您可以使用以下谓词元素定义密码复杂性:
-
IsLengthBetween8And64 验证
IsLengthRange
密码是否必须介于 8 到 64 个字符之间。 - 使用
IncludesCharacters
该方法的 Lowercase 验证密码是否包含小写字母。 -
Uppercase 使用该方法
IncludesCharacters
,验证密码是否包含大写字母。 -
Number 使用该方法
IncludesCharacters
验证密码是否包含数字。 -
Symbol 使用该方法
IncludesCharacters
,验证密码是否包含多个 symbol 字符之一。 -
PIN 验证
MatchesRegex
密码是否仅包含数字。 -
AllowedAADCharacters 使用该方法
MatchesRegex
,验证是否仅提供了密码无效字符。 -
DisallowedWhitespace 使用该方法
MatchesRegex
,验证密码是否不以空格字符开头或结尾。
<Predicates>
<Predicate Id="IsLengthBetween8And64" Method="IsLengthRange" HelpText="The password must be between 8 and 64 characters.">
<Parameters>
<Parameter Id="Minimum">8</Parameter>
<Parameter Id="Maximum">64</Parameter>
</Parameters>
</Predicate>
<Predicate Id="Lowercase" Method="IncludesCharacters" HelpText="a lowercase letter">
<Parameters>
<Parameter Id="CharacterSet">a-z</Parameter>
</Parameters>
</Predicate>
<Predicate Id="Uppercase" Method="IncludesCharacters" HelpText="an uppercase letter">
<Parameters>
<Parameter Id="CharacterSet">A-Z</Parameter>
</Parameters>
</Predicate>
<Predicate Id="Number" Method="IncludesCharacters" HelpText="a digit">
<Parameters>
<Parameter Id="CharacterSet">0-9</Parameter>
</Parameters>
</Predicate>
<Predicate Id="Symbol" Method="IncludesCharacters" HelpText="a symbol">
<Parameters>
<Parameter Id="CharacterSet">@#$%^&*\-_+=[]{}|\\:',.?/`~"();!</Parameter>
</Parameters>
</Predicate>
<Predicate Id="PIN" Method="MatchesRegex" HelpText="The password must be numbers only.">
<Parameters>
<Parameter Id="RegularExpression">^[0-9]+$</Parameter>
</Parameters>
</Predicate>
<Predicate Id="AllowedAADCharacters" Method="MatchesRegex" HelpText="An invalid character was provided.">
<Parameters>
<Parameter Id="RegularExpression">(^([0-9A-Za-z\d@#$%^&*\-_+=[\]{}|\\:',?/`~"();! ]|(\.(?!@)))+$)|(^$)</Parameter>
</Parameters>
</Predicate>
<Predicate Id="DisallowedWhitespace" Method="MatchesRegex" HelpText="The password must not begin or end with a whitespace character.">
<Parameters>
<Parameter Id="RegularExpression">(^\S.*\S$)|(^\S+$)|(^$)</Parameter>
</Parameters>
</Predicate>
定义基本验证后,您可以将它们组合在一起,并创建一组可在策略中使用的密码策略:
- SimplePassword 验证 DisallowedWhitespace、AllowedAADCharacters 和 IsLengthBetween8And64
-
StrongPassword 验证 DisallowedWhitespace、AllowedAADCharacters、IsLengthBetween8And64。 最后一个组
CharacterClasses
运行一组设置为 3 的附加谓词MatchAtLeast
。 用户密码必须介于 8 到 16 个字符之间,并且是以下字符中的三个:小写、大写、数字或符号。 - CustomPassword 仅验证 DisallowedWhitespace、AllowedAADCharacters。 因此,只要字符有效,用户就可以提供任何长度的密码。
<PredicateValidations>
<PredicateValidation Id="SimplePassword">
<PredicateGroups>
<PredicateGroup Id="DisallowedWhitespaceGroup">
<PredicateReferences>
<PredicateReference Id="DisallowedWhitespace" />
</PredicateReferences>
</PredicateGroup>
<PredicateGroup Id="AllowedAADCharactersGroup">
<PredicateReferences>
<PredicateReference Id="AllowedAADCharacters" />
</PredicateReferences>
</PredicateGroup>
<PredicateGroup Id="LengthGroup">
<PredicateReferences>
<PredicateReference Id="IsLengthBetween8And64" />
</PredicateReferences>
</PredicateGroup>
</PredicateGroups>
</PredicateValidation>
<PredicateValidation Id="StrongPassword">
<PredicateGroups>
<PredicateGroup Id="DisallowedWhitespaceGroup">
<PredicateReferences>
<PredicateReference Id="DisallowedWhitespace" />
</PredicateReferences>
</PredicateGroup>
<PredicateGroup Id="AllowedAADCharactersGroup">
<PredicateReferences>
<PredicateReference Id="AllowedAADCharacters" />
</PredicateReferences>
</PredicateGroup>
<PredicateGroup Id="LengthGroup">
<PredicateReferences>
<PredicateReference Id="IsLengthBetween8And64" />
</PredicateReferences>
</PredicateGroup>
<PredicateGroup Id="CharacterClasses">
<UserHelpText>The password must have at least 3 of the following:</UserHelpText>
<PredicateReferences MatchAtLeast="3">
<PredicateReference Id="Lowercase" />
<PredicateReference Id="Uppercase" />
<PredicateReference Id="Number" />
<PredicateReference Id="Symbol" />
</PredicateReferences>
</PredicateGroup>
</PredicateGroups>
</PredicateValidation>
<PredicateValidation Id="CustomPassword">
<PredicateGroups>
<PredicateGroup Id="DisallowedWhitespaceGroup">
<PredicateReferences>
<PredicateReference Id="DisallowedWhitespace" />
</PredicateReferences>
</PredicateGroup>
<PredicateGroup Id="AllowedAADCharactersGroup">
<PredicateReferences>
<PredicateReference Id="AllowedAADCharacters" />
</PredicateReferences>
</PredicateGroup>
</PredicateGroups>
</PredicateValidation>
</PredicateValidations>
在您的声明类型中,添加 PredicateValidationReference 元素并将标识符指定为谓词验证之一,例如 SimplePassword、StrongPassword 或 CustomPassword。
<ClaimType Id="password">
<DisplayName>Password</DisplayName>
<DataType>string</DataType>
<AdminHelpText>Enter password</AdminHelpText>
<UserHelpText>Enter password</UserHelpText>
<UserInputType>Password</UserInputType>
<PredicateValidationReference Id="StrongPassword" />
</ClaimType>
下面显示了当 Azure AD B2C 显示错误消息时如何组织元素:
配置日期范围
使用 Predicates 和 PredicateValidations 元素,您可以使用 .DateTimeDropdown
为此,请使用IsDateRange
该方法创建 Predicate 并提供 minimum 和 maximum 参数。
<Predicates>
<Predicate Id="DateRange" Method="IsDateRange" HelpText="The date must be between 01-01-1980 and today.">
<Parameters>
<Parameter Id="Minimum">1980-01-01</Parameter>
<Parameter Id="Maximum">Today</Parameter>
</Parameters>
</Predicate>
</Predicates>
添加一个 PredicateValidation 并引用该 DateRange
谓词。
<PredicateValidations>
<PredicateValidation Id="CustomDateRange">
<PredicateGroups>
<PredicateGroup Id="DateRangeGroup">
<PredicateReferences>
<PredicateReference Id="DateRange" />
</PredicateReferences>
</PredicateGroup>
</PredicateGroups>
</PredicateValidation>
</PredicateValidations>
在声明类型中,添加 PredicateValidationReference 元素并将标识符指定为 CustomDateRange
.
<ClaimType Id="dateOfBirth">
<DisplayName>Date of Birth</DisplayName>
<DataType>date</DataType>
<AdminHelpText>The user's date of birth.</AdminHelpText>
<UserHelpText>Your date of birth.</UserHelpText>
<UserInputType>DateTimeDropdown</UserInputType>
<PredicateValidationReference Id="CustomDateRange" />
</ClaimType>
后续步骤
- 了解如何使用谓词验证 在 Azure Active Directory B2C 中使用自定义策略配置密码复杂性 。