日期声明转换Date claims transformations

备注

在 Azure Active Directory B2C 中,custom policies 主要用于解决复杂方案。In Azure Active Directory B2C, custom policies are designed primarily to address complex scenarios. 大多数情况下,建议使用内置的用户流For most scenarios, we recommend that you use built-in user flows.

本文提供了在 Azure Active Directory B2C (Azure AD B2C) 中使用标识体验框架架构的日期声明转换的示例。This article provides examples for using the date claims transformations of the Identity Experience Framework schema in Azure Active Directory B2C (Azure AD B2C). 有关详细信息,请参阅 ClaimsTransformationsFor more information, see ClaimsTransformations.

AssertDateTimeIsGreaterThanAssertDateTimeIsGreaterThan

检查一个日期和时间声明(字符串数据类型)是否晚于另一个日期和时间声明(字符串数据类型),然后引发异常。Checks that one date and time claim (string data type) is later than a second date and time claim (string data type), and throws an exception.

项目Item TransformationClaimTypeTransformationClaimType 数据类型Data Type 注释Notes
InputClaimInputClaim leftOperandleftOperand stringstring 第一个声明的类型,应晚于第二个声明。First claim's type, which should be later than the second claim.
InputClaimInputClaim rightOperandrightOperand stringstring 第二个声明的类型,应早于第一个声明。Second claim's type, which should be earlier than the first claim.
InputParameterInputParameter AssertIfEqualToAssertIfEqualTo booleanboolean 指定如果左操作数等于右操作数,是否应传递此断言。Specifies whether this assertion should pass if the left operand is equal to the right operand.
InputParameterInputParameter AssertIfRightOperandIsNotPresentAssertIfRightOperandIsNotPresent booleanboolean 指定如果缺少右操作数,是否应传递此断言。Specifies whether this assertion should pass if the right operand is missing.
InputParameterInputParameter TreatAsEqualIfWithinMillsecondsTreatAsEqualIfWithinMillseconds intint 指定将两个日期时间视为相等时允许两者之间相差的毫秒数(例如,用于说明时钟偏差)。Specifies the number of milliseconds to allow between the two date times to consider the times equal (for example, to account for clock skew).

AssertDateTimeIsGreaterThan 声明转换始终从验证技术配置文件执行,该文件由自断言技术配置文件调用。The AssertDateTimeIsGreaterThan claims transformation is always executed from a validation technical profile that is called by a self-asserted technical profile. DateTimeGreaterThan 自断言技术配置文件元数据控制技术配置文件向用户呈现的错误消息。The DateTimeGreaterThan self-asserted technical profile metadata controls the error message that the technical profile presents to the user. 可以将错误消息本地化The error messages can be localized.

AssertStringClaimsAreEqual 执行

下面的示例比较 currentDateTime 声明与 approvedDateTime 声明。The following example compares the currentDateTime claim with the approvedDateTime claim. 如果 currentDateTime 晚于 approvedDateTime,则引发错误。An error is thrown if currentDateTime is later than approvedDateTime. 如果它们的差异在 5 分钟(30000 毫秒)内,则转换将值视为相等。The transformation treats values as equal if they are within 5 minutes (30000 milliseconds) difference.

<ClaimsTransformation Id="AssertApprovedDateTimeLaterThanCurrentDateTime" TransformationMethod="AssertDateTimeIsGreaterThan">
  <InputClaims>
    <InputClaim ClaimTypeReferenceId="approvedDateTime" TransformationClaimType="leftOperand" />
    <InputClaim ClaimTypeReferenceId="currentDateTime" TransformationClaimType="rightOperand" />
  </InputClaims>
  <InputParameters>
    <InputParameter Id="AssertIfEqualTo" DataType="boolean" Value="false" />
    <InputParameter Id="AssertIfRightOperandIsNotPresent" DataType="boolean" Value="true" />
    <InputParameter Id="TreatAsEqualIfWithinMillseconds" DataType="int" Value="300000" />
  </InputParameters>
</ClaimsTransformation>

login-NonInteractive 验证技术配置文件会调用 AssertApprovedDateTimeLaterThanCurrentDateTime 声明转换。The login-NonInteractive validation technical profile calls the AssertApprovedDateTimeLaterThanCurrentDateTime claims transformation.

<TechnicalProfile Id="login-NonInteractive">
  ...
  <OutputClaimsTransformations>
    <OutputClaimsTransformation ReferenceId="AssertApprovedDateTimeLaterThanCurrentDateTime" />
  </OutputClaimsTransformations>
</TechnicalProfile>

自断言技术配置文件调用验证 login-NonInteractive 技术配置文件。The self-asserted technical profile calls the validation login-NonInteractive technical profile.

<TechnicalProfile Id="SelfAsserted-LocalAccountSignin-Email">
  <Metadata>
    <Item Key="DateTimeGreaterThan">Custom error message if the provided left operand is greater than the right operand.</Item>
  </Metadata>
  <ValidationTechnicalProfiles>
    <ValidationTechnicalProfile ReferenceId="login-NonInteractive" />
  </ValidationTechnicalProfiles>
</TechnicalProfile>

示例Example

  • 输入声明:Input claims:
    • leftOperand :2020-03-01T15:00:00.0000000ZleftOperand: 2020-03-01T15:00:00.0000000Z
    • rightOperand :2020-03-01T14:00:00.0000000ZrightOperand: 2020-03-01T14:00:00.0000000Z
  • 结果:引发错误Result: Error thrown

ConvertDateToDateTimeClaimConvertDateToDateTimeClaim

将 Date ClaimType 转换为 DateTime ClaimType。Converts a Date ClaimType to a DateTime ClaimType. 该声明转换会转换时间格式并向日期添加 12:00:00 AM。The claims transformation converts the time format and adds 12:00:00 AM to the date.

项目Item TransformationClaimTypeTransformationClaimType 数据类型Data Type 注释Notes
InputClaimInputClaim inputClaiminputClaim datedate 要转换的 ClaimType。The ClaimType to be converted.
OutputClaimOutputClaim outputClaimoutputClaim dateTimedateTime 调用此 ClaimsTransformation 后生成的 ClaimType。The ClaimType that is produced after this ClaimsTransformation has been invoked.

下面的示例演示声明 dateOfBirth(date 数据类型)到另一个声明 dateOfBirthWithTime(dateTime 数据类型)的转换。The following example demonstrates the conversion of the claim dateOfBirth (date data type) to another claim dateOfBirthWithTime (dateTime data type).

  <ClaimsTransformation Id="ConvertToDateTime" TransformationMethod="ConvertDateToDateTimeClaim">
    <InputClaims>
      <InputClaim ClaimTypeReferenceId="dateOfBirth" TransformationClaimType="inputClaim" />
    </InputClaims>
    <OutputClaims>
      <OutputClaim ClaimTypeReferenceId="dateOfBirthWithTime" TransformationClaimType="outputClaim" />
    </OutputClaims>
  </ClaimsTransformation>

示例Example

  • 输入声明:Input claims:
    • inputClaim:2020-15-03inputClaim: 2020-15-03
  • 输出声明:Output claims:
    • outputClaim:2020-15-03T00:00:00.0000000ZoutputClaim: 2020-15-03T00:00:00.0000000Z

ConvertDateTimeToDateClaimConvertDateTimeToDateClaim

DateTime ClaimType 转换为 Date ClaimType。Converts a DateTime ClaimType to a Date ClaimType. 声明转换会从日期中删除时间格式。The claims transformation removes the time format from the date.

项目Item TransformationClaimTypeTransformationClaimType 数据类型Data Type 注释Notes
InputClaimInputClaim inputClaiminputClaim dateTimedateTime 要转换的 ClaimType。The ClaimType to be converted.
OutputClaimOutputClaim outputClaimoutputClaim datedate 调用此 ClaimsTransformation 后生成的 ClaimType。The ClaimType that is produced after this ClaimsTransformation has been invoked.

以下示例演示如何将声明 systemDateTime(日期/时间数据类型)转换为另一个声明systemDate(日期数据类型)。The following example demonstrates the conversion of the claim systemDateTime (dateTime data type) to another claim systemDate (date data type).

<ClaimsTransformation Id="ConvertToDate" TransformationMethod="ConvertDateTimeToDateClaim">
  <InputClaims>
    <InputClaim ClaimTypeReferenceId="systemDateTime" TransformationClaimType="inputClaim" />
  </InputClaims>
  <OutputClaims>
    <OutputClaim ClaimTypeReferenceId="systemDate" TransformationClaimType="outputClaim" />
  </OutputClaims>
</ClaimsTransformation>

示例Example

  • 输入声明:Input claims:
    • inputClaim:2020-15-03T11:34:22.0000000ZinputClaim: 2020-15-03T11:34:22.0000000Z
  • 输出声明:Output claims:
    • outputClaim:2020-15-03outputClaim: 2020-15-03

GetCurrentDateTimeGetCurrentDateTime

获取当前 UTC 日期和时间,并将值添加到 ClaimType。Get the current UTC date and time and add the value to a ClaimType.

项目Item TransformationClaimTypeTransformationClaimType 数据类型Data Type 注释Notes
OutputClaimOutputClaim currentDateTimecurrentDateTime dateTimedateTime 调用此 ClaimsTransformation 后生成的 ClaimType。The ClaimType that is produced after this ClaimsTransformation has been invoked.
<ClaimsTransformation Id="GetSystemDateTime" TransformationMethod="GetCurrentDateTime">
  <OutputClaims>
    <OutputClaim ClaimTypeReferenceId="systemDateTime" TransformationClaimType="currentDateTime" />
  </OutputClaims>
</ClaimsTransformation>

示例Example

  • 输出声明:Output claims:
    • currentDateTime :2020-15-03T11:40:35.0000000ZcurrentDateTime: 2020-15-03T11:40:35.0000000Z

DateTimeComparisonDateTimeComparison

确定一个日期/时间是晚于、早于还是等于另一个日期/时间。Determine whether one dateTime is later, earlier, or equal to another. 结果是值为 truefalse 的新布尔 ClaimType 布尔值。The result is a new boolean ClaimType boolean with a value of true or false.

项目Item TransformationClaimTypeTransformationClaimType 数据类型Data Type 注释Notes
InputClaimInputClaim firstDateTimefirstDateTime dateTimedateTime 第一个日期/时间,用于比较它是在第二个日期/时间之前还是之后。The first dateTime to compare whether it is earlier or later than the second dateTime. Null 值会引发异常。Null value throws an exception.
InputClaimInputClaim secondDateTimesecondDateTime dateTimedateTime 第二个日期/时间,用于比较它是在第一个日期/时间之前还是之后。The second dateTime to compare whether it is earlier or later than the first dateTime. NULL 值被视为当前日期/时间。Null value is treated as the current datetTime.
InputParameterInputParameter operatoroperator stringstring 以下值之一:same、later than 或 earlier than。One of following values: same, later than, or earlier than.
InputParameterInputParameter timeSpanInSecondstimeSpanInSeconds intint 向第一个日期/时间添加的时间范围。Add the timespan to the first datetime.
OutputClaimOutputClaim resultresult booleanboolean 调用此 ClaimsTransformation 后生成的 ClaimType。The ClaimType that is produced after this ClaimsTransformation has been invoked.

使用此声明转换可确定两个 ClaimType 之间是相等、晚于还是早于。Use this claims transformation to determine if two ClaimTypes are equal, later, or earlier than each other. 例如,可能会存储用户接受服务条款 (TOS) 的上次时间。For example, you may store the last time a user accepted your terms of services (TOS). 3 个月后,可以要求用户再次访问 TOS。After 3 months, you can ask the user to access the TOS again. 若要运行声明转换,首先需要获取当前日期/时间,以及用户接受 TOS 的上次时间。To run the claim transformation, you first need to get the current dateTime and also the last time user accepts the TOS.

<ClaimsTransformation Id="CompareLastTOSAcceptedWithCurrentDateTime" TransformationMethod="DateTimeComparison">
  <InputClaims>
    <InputClaim ClaimTypeReferenceId="currentDateTime" TransformationClaimType="firstDateTime" />
    <InputClaim ClaimTypeReferenceId="extension_LastTOSAccepted" TransformationClaimType="secondDateTime" />
  </InputClaims>
  <InputParameters>
    <InputParameter Id="operator" DataType="string" Value="later than" />
    <InputParameter Id="timeSpanInSeconds" DataType="int" Value="7776000" />
  </InputParameters>
  <OutputClaims>
    <OutputClaim ClaimTypeReferenceId="isLastTOSAcceptedGreaterThanNow" TransformationClaimType="result" />
  </OutputClaims>
</ClaimsTransformation>

示例Example

  • 输入声明:Input claims:
    • firstDateTime :2020-01-01T00:00:00.100000ZfirstDateTime: 2020-01-01T00:00:00.100000Z
    • secondDateTime :2020-04-01T00:00:00.100000ZsecondDateTime: 2020-04-01T00:00:00.100000Z
  • 输入参数:Input parameters:
    • operator: later than operator: later than
    • timeSpanInSeconds :7776000(90 天)timeSpanInSeconds: 7776000 (90 days)
  • 输出声明:Output claims:
    • 结果 :trueresult: true