JSON 声明转换JSON claims transformations

Note

在 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) 中使用标识体验框架架构的 JSON 声明转换的示例。This article provides examples for using the JSON claims transformations of the Identity Experience Framework schema in Azure Active Directory B2C (Azure AD B2C). 有关详细信息,请参阅 ClaimsTransformationsFor more information, see ClaimsTransformations.

GetClaimFromJsonGetClaimFromJson

从 JSON 数据中获取指定的元素。Get a specified element from a JSON data.

项目Item TransformationClaimTypeTransformationClaimType 数据类型Data Type 注释Notes
InputClaimInputClaim inputJsoninputJson stringstring 由声明转换用于获取项的 ClaimTypes。The ClaimTypes that are used by the claims transformation to get the item.
InputParameterInputParameter claimToExtractclaimToExtract stringstring 要提取的 JSON 元素的名称。the name of the JSON element to be extracted.
OutputClaimOutputClaim extractedClaimextractedClaim stringstring 调用此声明转换后生成的 ClaimType,即 claimToExtract 输入参数中指定的元素值。The ClaimType that is produced after this claims transformation has been invoked, the element value specified in the claimToExtract input parameter.

在以下示例中,声明转换提取 JSON 数据中的 emailAddress 元素:{"emailAddress": "someone@example.com", "displayName": "Someone"}In the following example, the claims transformation extracted the emailAddress element from the JSON data: {"emailAddress": "someone@example.com", "displayName": "Someone"}

<ClaimsTransformation Id="GetEmailClaimFromJson" TransformationMethod="GetClaimFromJson">
  <InputClaims>
    <InputClaim ClaimTypeReferenceId="customUserData" TransformationClaimType="inputJson" />
  </InputClaims>
  <InputParameters>
    <InputParameter Id="claimToExtract" DataType="string" Value="emailAddress" />
  </InputParameters>
  <OutputClaims>
    <OutputClaim ClaimTypeReferenceId="extractedEmail" TransformationClaimType="extractedClaim" />
  </OutputClaims>
</ClaimsTransformation>

示例Example

  • 输入声明:Input claims:
    • inputJson : {"emailAddress": "someone@example.com", "displayName":"Someone"}inputJson: {"emailAddress": "someone@example.com", "displayName": "Someone"}
  • 输入参数:Input parameter:
    • claimToExtract: emailAddressclaimToExtract: emailAddress
  • 输出声明:Output claims:
    • extractedClaim: someone@example.comextractedClaim: someone@example.com

GetClaimsFromJsonArrayGetClaimsFromJsonArray

从 Json 数据中获取指定元素列表。Get a list of specified elements from Json data.

项目Item TransformationClaimTypeTransformationClaimType 数据类型Data Type 注释Notes
InputClaimInputClaim jsonSourceClaimjsonSourceClaim stringstring 由声明转换用于获取声明的 ClaimTypes。The ClaimTypes that are used by the claims transformation to get the claims.
InputParameterInputParameter errorOnMissingClaimserrorOnMissingClaims 布尔值boolean 指定如果缺少一个声明是否引发错误。Specifies whether to throw an error if one of the claims is missing.
InputParameterInputParameter includeEmptyClaimsincludeEmptyClaims stringstring 指定是否包含空声明。Specify whether to include empty claims.
InputParameterInputParameter jsonSourceKeyNamejsonSourceKeyName stringstring 元素键名称Element key name
InputParameterInputParameter jsonSourceValueNamejsonSourceValueName stringstring 元素值名称Element value name
OutputClaimOutputClaim 集合Collection 字符串、int、布尔值,和日期时间string, int, boolean, and datetime 要提取的声明列表。List of claims to extract. 声明名称应等于 jsonSourceClaim 输入声明中指定的名称。The name of the claim should be equal to the one specified in jsonSourceClaim input claim.

在以下示例中,声明转换从 JSON 数据中提取以下声明:email(字符串)、displayName(字符串)、membershipNum (int)、active(布尔值)和 birthdate(日期时间)。In the following example, the claims transformation extracts the following claims: email (string), displayName (string), membershipNum (int), active (boolean) and birthdate (datetime) from the JSON data.

[{"key":"email","value":"someone@example.com"}, {"key":"displayName","value":"Someone"}, {"key":"membershipNum","value":6353399}, {"key":"active","value":true}, {"key":"birthdate","value":"1980-09-23T00:00:00Z"}]
<ClaimsTransformation Id="GetClaimsFromJson" TransformationMethod="GetClaimsFromJsonArray">
  <InputClaims>
    <InputClaim ClaimTypeReferenceId="jsonSourceClaim" TransformationClaimType="jsonSource" />
  </InputClaims>
  <InputParameters>
    <InputParameter Id="errorOnMissingClaims" DataType="boolean" Value="false" />
    <InputParameter Id="includeEmptyClaims" DataType="boolean" Value="false" />
    <InputParameter Id="jsonSourceKeyName" DataType="string" Value="key" />
    <InputParameter Id="jsonSourceValueName" DataType="string" Value="value" />
  </InputParameters>
  <OutputClaims>
    <OutputClaim ClaimTypeReferenceId="email" />
    <OutputClaim ClaimTypeReferenceId="displayName" />
    <OutputClaim ClaimTypeReferenceId="membershipNum" />
    <OutputClaim ClaimTypeReferenceId="active" />
    <OutputClaim ClaimTypeReferenceId="birthdate" />
  </OutputClaims>
</ClaimsTransformation>
  • 输入声明:Input claims:
    • jsonSourceClaim: [{"key":"email","value":"someone@example.com"}, {"key":"displayName","value":"Someone"}, {"key":"membershipNum","value":6353399}, {"key":"active","value": true}, {"key":"birthdate","value":"1980-09-23T00:00:00Z"}]jsonSourceClaim: [{"key":"email","value":"someone@example.com"}, {"key":"displayName","value":"Someone"}, {"key":"membershipNum","value":6353399}, {"key":"active","value": true}, {"key":"birthdate","value":"1980-09-23T00:00:00Z"}]
  • 输入参数:Input parameters:
    • errorOnMissingClaims: falseerrorOnMissingClaims: false
    • includeEmptyClaims: falseincludeEmptyClaims: false
    • jsonSourceKeyName: keyjsonSourceKeyName: key
    • jsonSourceValueName: valuejsonSourceValueName: value
  • 输出声明:Output claims:
    • email: "someone@example.com"email: "someone@example.com"
    • displayName :"Someone"displayName: "Someone"
    • membershipNum :6353399membershipNum: 6353399
    • active: trueactive: true
    • birthdate :1980-09-23T00:00:00Zbirthdate: 1980-09-23T00:00:00Z

GetNumericClaimFromJsonGetNumericClaimFromJson

从 JSON 数据中获取指定的数值 (long) 元素。Gets a specified numeric (long) element from a JSON data.

项目Item TransformationClaimTypeTransformationClaimType 数据类型Data Type 注释Notes
InputClaimInputClaim inputJsoninputJson stringstring 由声明转换用于获取声明的 ClaimTypes。The ClaimTypes that are used by the claims transformation to get the claim.
InputParameterInputParameter claimToExtractclaimToExtract stringstring 要提取的 JSON 元素的名称。The name of the JSON element to extract.
OutputClaimOutputClaim extractedClaimextractedClaim longlong 调用此 ClaimsTransformation 后生成的 ClaimType,即 claimToExtract 输入参数中指定的元素值。The ClaimType that is produced after this ClaimsTransformation has been invoked, the element's value specified in the claimToExtract input parameters.

在以下示例中,声明转换提取 JSON 数据中的 id 元素。In the following example, the claims transformation extracts the id element from the JSON data.

{
    "emailAddress": "someone@example.com",
    "displayName": "Someone",
    "id" : 6353399
}
<ClaimsTransformation Id="GetIdFromResponse" TransformationMethod="GetNumericClaimFromJson">
  <InputClaims>
    <InputClaim ClaimTypeReferenceId="exampleInputClaim" TransformationClaimType="inputJson" />
  </InputClaims>
  <InputParameters>
    <InputParameter Id="claimToExtract" DataType="string" Value="id" />
  </InputParameters>
  <OutputClaims>
    <OutputClaim ClaimTypeReferenceId="membershipId" TransformationClaimType="extractedClaim" />
  </OutputClaims>
</ClaimsTransformation>

示例Example

  • 输入声明:Input claims:
    • inputJson : {"emailAddress": "someone@example.com", "displayName":"Someone", "id" :6353399}inputJson: {"emailAddress": "someone@example.com", "displayName": "Someone", "id" : 6353399}
  • 输入参数Input parameters
    • claimToExtract: idclaimToExtract: id
  • 输出声明:Output claims:
    • extractedClaim:6353399extractedClaim: 6353399

GetSingleValueFromJsonArrayGetSingleValueFromJsonArray

从 JSON 数据数组中获取第一个元素。Gets the first element from a JSON data array.

项目Item TransformationClaimTypeTransformationClaimType 数据类型Data Type 注释Notes
InputClaimInputClaim inputJsonClaiminputJsonClaim stringstring 由声明转换用于从 JSON 数组中获取项的 ClaimTypes。The ClaimTypes that are used by the claims transformation to get the item from the JSON array.
OutputClaimOutputClaim extractedClaimextractedClaim stringstring 调用此 ClaimsTransformation 后生成的 ClaimType,即 JSON 数组中的第一个元素。The ClaimType that is produced after this ClaimsTransformation has been invoked, the first element in the JSON array.

在以下示例中,声明转换提取 JSON 数组 ["someone@example.com", "Someone", 6353399] 中的第一个元素(电子邮件地址)。In the following example, the claims transformation extracts the first element (email address) from the JSON array ["someone@example.com", "Someone", 6353399].

<ClaimsTransformation Id="GetEmailFromJson" TransformationMethod="GetSingleValueFromJsonArray">
  <InputClaims>
    <InputClaim ClaimTypeReferenceId="userData" TransformationClaimType="inputJsonClaim" />
  </InputClaims>
  <OutputClaims>
    <OutputClaim ClaimTypeReferenceId="email" TransformationClaimType="extractedClaim" />
  </OutputClaims>
</ClaimsTransformation>

示例Example

  • 输入声明:Input claims:
    • inputJsonClaim: ["someone@example.com", "Someone", 6353399]inputJsonClaim: ["someone@example.com", "Someone", 6353399]
  • 输出声明:Output claims:
    • extractedClaim: someone@example.comextractedClaim: someone@example.com

XmlStringToJsonStringXmlStringToJsonString

将 XML 数据转换为 JSON 格式。Converts XML data to JSON format.

项目Item TransformationClaimTypeTransformationClaimType 数据类型Data Type 注释Notes
InputClaimInputClaim xmlxml stringstring 由声明转换用于将数据从 XML 转换为 JSON 格式的 ClaimTypes。The ClaimTypes that are used by the claims transformation to convert the data from XML to JSON format.
OutputClaimOutputClaim jsonjson stringstring 调用此 ClaimsTransformation 后生成的 ClaimType,即采用 JSON 格式的数据。The ClaimType that is produced after this ClaimsTransformation has been invoked, the data in JSON format.
<ClaimsTransformation Id="ConvertXmlToJson" TransformationMethod="XmlStringToJsonString">
  <InputClaims>
    <InputClaim ClaimTypeReferenceId="intpuXML" TransformationClaimType="xml" />
  </InputClaims>
  <OutputClaims>
    <OutputClaim ClaimTypeReferenceId="outputJson" TransformationClaimType="json" />
  </OutputClaims>
</ClaimsTransformation>

在以下示例中,声明转换将以下 XML 数据转换为 JSON 格式。In the following example, the claims transformation converts the following XML data to JSON format.

示例Example

输入声明:Input claim:

<user>
  <name>Someone</name>
  <email>someone@example.com</email>
</user>

输出声明:Output claim:

{
  "user": {
    "name":"Someone",
    "email":"someone@example.com"
  }
}