這很重要
自 2025 年 5 月 1 日起,Azure AD B2C 將不再可供新客戶購買。 在我們的常見問題中深入瞭解。
本文提供在 Azure Active Directory B2C (Azure AD B2C) 中使用身分識別體驗架構架構的日期宣告轉換範例。 如需詳細資訊,請參閱 宣告轉換。
AssertDateTimeIsGreaterThan
判斷提示一個日期晚於第二個日期。 判斷 是否 rightOperand 大於 leftOperand。 如果是,則會擲回例外狀況。 查看 此宣告轉換的實時示範 。
| 元素 | TransformationClaimType | 數據類型 | 註釋 |
|---|---|---|---|
| InputClaim (輸入宣告) | leftOper數 | 字符串 | 第一個宣告的類型,應該晚於第二個宣告。 |
| InputClaim (輸入宣告) | rightOper數 | 字符串 | 第二個宣告的類型,應該早於第一個宣告。 |
| 輸入參數 | AssertIfEqualTo | 布爾值 | 指定如果左作數等於右作數,這個判斷提示是否應該擲回錯誤。 可能的值: true (預設值),或 false。 |
| 輸入參數 | AssertIfRightOperandIsNotPresent | 布爾值 | 指定如果遺漏右作數,是否應該傳遞這個判斷提示。 |
| 輸入參數 | TreatAsEqualIfWithinMillseconds | 整數 (int) | 指定要在兩個日期時間之間考慮時間相等的毫秒數(例如,考慮時鐘扭曲)。 |
AssertDateTimeIsGreaterThan 宣告轉換一律會從自我判斷技術配置檔呼叫的驗證技術配置文件執行。 DateTimeGreaterThan 自我判斷技術配置檔元數據會控制技術配置檔向用戶呈現的錯誤訊息。 錯誤訊息可以 當地語系化。
AssertDateTimeIsGreaterThan 的範例
下列範例會 currentDateTime 比較宣告與 approvedDateTime 宣告。 如果 currentDateTime 晚於 approvedDateTime,則會擲回錯誤。 如果值在 5 分鐘內(30000 毫秒)的差異,則轉換會將值視為相等。 如果值相等,則不會擲回錯誤,因為 AssertIfEqualTo 設定為 false。
<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>
備註
在上述範例中,如果您移除AssertIfEqualTo輸入參數,且 currentDateTimeapprovedDateTime等於 ,則會擲回錯誤。
AssertIfEqualTo預設值為 true。
- 輸入宣告:
- leftOperand: 2022-01-01T15:00:00
- rightOperand: 2022-01-22T15:00:00
- 輸入參數:
- AssertIfEqualTo: false
- AssertIfRightOperandIsNotPresent: true
- TreatAsEqualIfWithinMillseconds: 300000 (30 秒)
- 結果:擲回錯誤
呼叫宣告轉換
下列 Example-AssertDates 驗證技術配置檔會呼叫 AssertApprovedDateTimeLaterThanCurrentDateTime 宣告轉換。
<TechnicalProfile Id="Example-AssertDates">
<DisplayName>Unit test</DisplayName>
<Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.ClaimsTransformationProtocolProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<OutputClaims>
<OutputClaim ClaimTypeReferenceId="ComparisonResult" DefaultValue="false" />
</OutputClaims>
<OutputClaimsTransformations>
<OutputClaimsTransformation ReferenceId="AssertDates" />
</OutputClaimsTransformations>
<UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
</TechnicalProfile>
自我判斷技術配置檔會呼叫驗證 Example-AssertDates 技術配置檔。
<TechnicalProfile Id="SelfAsserted-AssertDateTimeIsGreaterThan">
<DisplayName>User ID signup</DisplayName>
<Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.SelfAssertedAttributeProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Metadata>
<Item Key="ContentDefinitionReferenceId">api.selfasserted</Item>
<Item Key="DateTimeGreaterThan">Custom error message if the provided right operand is greater than the right operand.</Item>
</Metadata>
...
<ValidationTechnicalProfiles>
<ValidationTechnicalProfile ReferenceId="ClaimsTransformation-AssertDateTimeIsGreaterThan" />
</ValidationTechnicalProfiles>
</TechnicalProfile>
ConvertDateTimeToDateClaim
將 DateTime 宣告類型轉換為 Date 宣告類型。 宣告轉換會從日期移除時間格式。 查看 此宣告轉換的實時示範 。
| 元素 | TransformationClaimType | 數據類型 | 註釋 |
|---|---|---|---|
| InputClaim (輸入宣告) | inputClaim | 日期時間 | 要轉換的宣告類型。 |
| 輸出聲明 | outputClaim | 日期 | 叫用這個宣告轉換之後所產生的宣告類型。 |
ConvertDateTimeToDateClaim 的範例
下列範例示範如何將宣告 systemDateTime (dateTime 數據類型) 轉換成另一個宣告 systemDate (date 數據類型)。
<ClaimsTransformation Id="ConvertToDate" TransformationMethod="ConvertDateTimeToDateClaim">
<InputClaims>
<InputClaim ClaimTypeReferenceId="systemDateTime" TransformationClaimType="inputClaim" />
</InputClaims>
<OutputClaims>
<OutputClaim ClaimTypeReferenceId="systemDate" TransformationClaimType="outputClaim" />
</OutputClaims>
</ClaimsTransformation>
- 輸入宣告:
- inputClaim: 2022-01-03T11:34:22.0000000Z
- 輸出宣告:
- outputClaim: 2022-01-03
ConvertDateToDateTimeClaim
將 Date 宣告類型轉換為 DateTime 宣告類型。 宣告轉換會轉換時間格式,並將上午 12:00:00 新增至日期。 查看 此宣告轉換的實時示範 。
| 元素 | TransformationClaimType | 數據類型 | 註釋 |
|---|---|---|---|
| InputClaim (輸入宣告) | inputClaim | 日期 | 要轉換的宣告類型。 |
| 輸出聲明 | outputClaim | 日期時間 | 叫用這個宣告轉換之後所產生的宣告類型。 |
ConvertDateToDateTimeClaim 的範例
下列範例示範如何將宣告 dateOfBirth (date 數據類型) 轉換成另一個宣告 dateOfBirthWithTime (dateTime 數據類型)。
<ClaimsTransformation Id="ConvertToDateTime" TransformationMethod="ConvertDateToDateTimeClaim">
<InputClaims>
<InputClaim ClaimTypeReferenceId="dateOfBirth" TransformationClaimType="inputClaim" />
</InputClaims>
<OutputClaims>
<OutputClaim ClaimTypeReferenceId="dateOfBirthWithTime" TransformationClaimType="outputClaim" />
</OutputClaims>
</ClaimsTransformation>
- 輸入宣告:
- inputClaim:2022-01-03
- 輸出宣告:
- outputClaim: 2022-01-03T00:00:00.0000000Z
DateTimeComparison
比較兩個日期,並判斷第一個日期是較晚、更早或等於另一個日期。 結果是新的布林值宣告,值為 true 或 false。 查看 此宣告轉換的實時示範 。
| 元素 | TransformationClaimType | 數據類型 | 註釋 |
|---|---|---|---|
| InputClaim (輸入宣告) | firstDateTime (首次日期時間) | 日期時間 | 要比較的第一個日期是較晚、更早或等於第二個日期。 Null 值會擲回例外狀況。 |
| InputClaim (輸入宣告) | secondDateTime (第二日期時間) | 日期時間 | 要比較的第二個日期。 Null 值會視為目前的日期和時間。 |
| 輸入參數 | 時間跨度秒 | 整數 (int) | 要新增至第一個日期的 Timespan。 可能的值:從負 -2,147,483,648 到正 2,147,483,647 的範圍。 |
| 輸入參數 | 操作員 | 字符串 | 下列其中一個值: same、 later than或 earlier than。 |
| 輸出聲明 | 結果 | 布爾值 | 叫用這個宣告轉換之後所產生的宣告。 |
DateTimeComparison 的範例
使用此宣告轉換來判斷第一個日期加上 timeSpanInSeconds 參數是否在稍後、更早或等於另一個日期。 下列範例顯示第一個日期 (2022-01-01T00:00:00) 加上 90 天晚於第二個日期 (2022-03-16T00:00:00)。
<ClaimsTransformation Id="CompareLastTOSAcceptedWithCurrentDateTime" TransformationMethod="DateTimeComparison">
<InputClaims>
<InputClaim ClaimTypeReferenceId="extension_LastTOSAccepted" TransformationClaimType="secondDateTime" />
<InputClaim ClaimTypeReferenceId="currentDateTime" TransformationClaimType="firstDateTime" />
</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>
- 輸入宣告:
- firstDateTime: 2022-01-01T00:00:00.100000Z
- secondDateTime: 2022-03-16T00:00:00.100000Z
- 輸入參數:
- 運算子:晚於
- timeSpanInSeconds: 7776000 (90 天)
- 輸出宣告:
- result: true
IsTermsOfUseConsentRequired
dateTime判斷宣告類型早於或大於特定日期。 結果是新的布林值宣告,值為 true 或 false。 查看 此宣告轉換的實時示範 。
| 項目 | TransformationClaimType | 數據類型 | 註釋 |
|---|---|---|---|
| InputClaim (輸入宣告) | termsOfUseConsentDateTime | 日期時間 | 檢查 dateTime 其是否早於輸入參數或更新版本的 termsOfUseTextUpdateDateTime 宣告類型。 未定義的值會傳 true 回結果。 |
| 輸入參數 | termsOfUseTextUpdateDateTime | 日期時間 |
dateTime檢查其是否早於或晚於輸入宣告的termsOfUseConsentDateTime宣告類型。 日期的時間部分是選擇性的。 |
| 輸出聲明 | 結果 | 布爾值 | 叫用這個宣告轉換之後所產生的宣告類型。 |
使用此宣告轉換來判斷 dateTime 宣告類型早於或大於特定日期。 例如,檢查使用者是否已同意最新版的使用規定 (TOU) 或服務條款。 若要檢查使用者上次同意的時間,請將使用者上次接受 TOU 的時間儲存在 擴充屬性中。 當您的 TOU 字組變更時,請使用變更的時間更新 termsOfUseTextUpdateDateTime 輸入參數。 然後,呼叫此宣告轉換來比較日期。 如果宣告轉換傳 true回 ,則 termsOfUseConsentDateTime 此值早於 termsOfUseTextUpdateDateTime 值,而且您可以要求使用者接受更新的 TOU。
<ClaimsTransformation Id="IsTermsOfUseConsentRequired" TransformationMethod="IsTermsOfUseConsentRequired">
<InputClaims>
<InputClaim ClaimTypeReferenceId="extension_termsOfUseConsentDateTime" TransformationClaimType="termsOfUseConsentDateTime" />
</InputClaims>
<InputParameters>
<InputParameter Id="termsOfUseTextUpdateDateTime" DataType="dateTime" Value="2021-11-15T00:00:00" />
</InputParameters>
<OutputClaims>
<OutputClaim ClaimTypeReferenceId="termsOfUseConsentRequired" TransformationClaimType="result" />
</OutputClaims>
</ClaimsTransformation>
IsTermsOfUseConsentRequired 範例
- 輸入宣告:
- termsOfUseConsentDateTime: 2020-03-09T09:15:00
- 輸入參數:
- termsOfUseTextUpdateDateTime: 2021-11-15
- 輸出宣告:
- result: true
取得當前日期時間
取得目前的 UTC 日期和時間,並將值新增至宣告類型。 查看 此宣告轉換的實時示範 。
| 元素 | TransformationClaimType | 數據類型 | 註釋 |
|---|---|---|---|
| 輸出聲明 | 當前日期時間 | 日期時間 | 叫用這個宣告轉換之後所產生的宣告類型。 |
GetCurrentDateTime 的範例
下列範例示範如何取得目前的數據和時間:
<ClaimsTransformation Id="GetSystemDateTime" TransformationMethod="GetCurrentDateTime">
<OutputClaims>
<OutputClaim ClaimTypeReferenceId="systemDateTime" TransformationClaimType="currentDateTime" />
</OutputClaims>
</ClaimsTransformation>
- 輸出宣告:
- currentDateTime: 2022-01-14T11:40:35.0000000Z
後續步驟
- 在 Azure AD B2C 社群 GitHub 存放庫上尋找更多宣告轉換範例