你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

在 Azure Active Directory B2C 自定义策略中定义声明转换技术配置文件

注意

在 Azure Active Directory B2C 中,自定义策略主要用于解决复杂的情况。 在大多数情况下,建议你使用内置用户流。 如果尚未这样做,请从 Active Directory B2C 中的自定义策略入门了解自定义策略新手包。

使用声明转换技术配置文件,可以通过调用输出声明转换针对一组输出声明来操作声明值、验证声明或设置默认值。

协议

“Protocol”元素的“Name”属性必须设置为 Proprietaryhandler 属性必须包含 Azure AD B2C 使用的协议处理程序程序集的完全限定名称:Web.TPEngine.Providers.ClaimsTransformationProtocolProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

以下示例演示了一个声明转换技术配置文件:

<TechnicalProfile Id="Facebook-OAUTH-UnLink">
    <DisplayName>Unlink Facebook</DisplayName>
    <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.ClaimsTransformationProtocolProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  ...

输出声明

OutputClaims 元素是必需的。 提供至少一个由技术配置文件返回的输出声明。 以下示例说明了如何在输出声明中设置默认值:

<OutputClaims>
  <OutputClaim ClaimTypeReferenceId="ageGroup" DefaultValue="Undefined" />
  <OutputClaim ClaimTypeReferenceId="ageGroupValueChanged" DefaultValue="false" />
</OutputClaims>

输出声明转换

OutputClaimsTransformations 元素可能包含用于修改声明或生成新声明的 OutputClaimsTransformation 元素集合。 以下技术配置文件调用 RemoveAlternativeSecurityIdByIdentityProvider 声明转换。 此声明转换从 AlternativeSecurityIds 的集合中删除一个社交标识。 此技术配置文件的输出声明为 identityProvider2(设置为 facebook.com)和 AlternativeSecurityIds(其中包含在删除 facebook.com 标识后与此用户关联的社交标识的列表)。

<ClaimsTransformations>
  <ClaimsTransformation Id="RemoveAlternativeSecurityIdByIdentityProvider"
TransformationMethod="RemoveAlternativeSecurityIdByIdentityProvider">
    <InputClaims>
      <InputClaim ClaimTypeReferenceId="IdentityProvider2"
TransformationClaimType="identityProvider" />
      <InputClaim ClaimTypeReferenceId="AlternativeSecurityIds"
TransformationClaimType="collection" />
    </InputClaims>
    <OutputClaims>
      <OutputClaim ClaimTypeReferenceId="AlternativeSecurityIds"
TransformationClaimType="collection" />
    </OutputClaims>
  </ClaimsTransformation>
</ClaimsTransformations>
...
<TechnicalProfile Id="Facebook-OAUTH-UnLink">
    <DisplayName>Unlink Facebook</DisplayName>
    <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.ClaimsTransformationProtocolProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    <OutputClaims>
        <OutputClaim ClaimTypeReferenceId="identityProvider2" DefaultValue="facebook.com" AlwaysUseDefaultValue="true" />
    </OutputClaims>
    <OutputClaimsTransformations>
        <OutputClaimsTransformation ReferenceId="RemoveAlternativeSecurityIdByIdentityProvider" />
    </OutputClaimsTransformations>
    <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
</TechnicalProfile>

使用声明转换技术配置文件,可以在任何用户旅程的业务流程步骤中执行声明转换。 在以下示例中,业务流程步骤调用取消链接技术配置文件中的一个,例如 UnLink-Facebook-OAUTH。 此技术配置文件调用输出声明转换 RemoveAlternativeSecurityIdByIdentityProvider,它会生成新的 AlternativeSecurityIds2 声明。 输出声明包含用户的社交身份列表,同时从集合中删除 Facebook 身份。

<UserJourney Id="AccountUnLink">
  <OrchestrationSteps>
    ...
    <OrchestrationStep Order="8" Type="ClaimsExchange">
      <ClaimsExchanges>
        <ClaimsExchange Id="UnLinkFacebookExchange" TechnicalProfileReferenceId="UnLink-Facebook-OAUTH" />
        <ClaimsExchange Id="UnLinkMicrosoftExchange" TechnicalProfileReferenceId="UnLink-Microsoft-OAUTH" />
        <ClaimsExchange Id="UnLinkGitHubExchange" TechnicalProfileReferenceId="UnLink-GitHub-OAUTH" />
      </ClaimsExchanges>
    </OrchestrationStep>
    ...
  </OrchestrationSteps>
</UserJourney>

元数据

Attribute 必选 说明
IncludeClaimResolvingInClaimsHandling 对于输入和输出声明,指定声明解析是否包含在技术配置文件中。 可能的值:truefalse(默认值)。 若要使用技术配置文件中的声明解析程序,请将此元数据设为 true
ContentDefinitionReferenceId 与此技术配置文件关联的内容定义的标识符。 FormatLocalizedStringGetLocalizedStringsTransformationGetMappedValueFromLocalizedCollection 声明转换需要内容定义元数据。

使用验证技术配置文件

声明转换技术配置文件可以用来验证信息。 在以下示例中,名为 LocalAccountSignUpWithLogonEmail自断言技术配置文件要求用户输入电子邮件两次,然后调用名为 Validate-Email验证技术配置文件,对电子邮件进行验证。 Validate-Email 技术配置文件调用声明转换 AssertEmailAreEqual 来比较两个声明(emailemailRepeat)。如果在进行指定的比较后确定这两个声明不相同,则会引发异常。

<ClaimsTransformations>
  <ClaimsTransformation Id="AssertEmailAreEqual" TransformationMethod="AssertStringClaimsAreEqual">
    <InputClaims>
      <InputClaim ClaimTypeReferenceId="email" TransformationClaimType="inputClaim1" />
      <InputClaim ClaimTypeReferenceId="emailRepeat" TransformationClaimType="inputClaim2" />
    </InputClaims>
    <InputParameters>
      <InputParameter Id="stringComparison" DataType="string" Value="ordinalIgnoreCase" />
    </InputParameters>
  </ClaimsTransformation>
</ClaimsTransformations>

声明转换技术配置文件调用 AssertEmailAreEqual 声明转换,后者断言用户提供的电子邮件是否相同。

<TechnicalProfile Id="Validate-Email">
  <DisplayName>Unlink Facebook</DisplayName>
  <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.ClaimsTransformationProtocolProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  <InputClaims>
    <InputClaim ClaimTypeReferenceId="emailRepeat" />
  </InputClaims>
  <OutputClaims>
    <OutputClaim ClaimTypeReferenceId="email" />
  </OutputClaims>
  <OutputClaimsTransformations>
    <OutputClaimsTransformation ReferenceId="AssertEmailAreEqual" />
  </OutputClaimsTransformations>
  <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
</TechnicalProfile>

自断言技术配置文件可以调用验证技术配置文件,并显示 UserMessageIfClaimsTransformationStringsAreNotEqual 元数据中指定的错误消息。

<TechnicalProfile Id="LocalAccountSignUpWithLogonEmail">
  <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="UserMessageIfClaimsTransformationStringsAreNotEqual">The email addresses you provided are not the same</Item>
  </Metadata>
  <OutputClaims>
    <OutputClaim ClaimTypeReferenceId="email" />
    <OutputClaim ClaimTypeReferenceId="emailRepeat" />
    ...
  </OutputClaims>
  <ValidationTechnicalProfiles>
    <ValidationTechnicalProfile ReferenceId="Validate-Email" />
  </ValidationTechnicalProfiles>
</TechnicalProfile>