You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure AD B2C自定义登录策略:API校验失败后页面展示错误

解决方案:将REST校验嵌入自我断言技术配置以展示页面错误

要在B2C登录页面展示API校验的错误信息,不能直接把REST技术配置作为独立编排步骤,得把它**嵌套到自我断言技术配置(负责渲染用户交互页面的组件)**里,这样校验失败时会停在登录页并友好提示。具体步骤如下:

1. 调整REST技术配置,支持错误传递

首先修改你的REST-CheckInactiveEmployee技术配置,添加错误处理元数据,确保API返回的错误能被B2C识别并传递到前端页面:

<ClaimsProvider>
  <DisplayName>Validate Inactive Employee</DisplayName>
  <TechnicalProfiles>
    <TechnicalProfile Id="REST-CheckInactiveEmployee">
      <DisplayName>Check Input Data</DisplayName>
      <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.RestfulProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      <Metadata>
        <Item Key="ServiceUrl">https://NAMEOFFUNCTIONGOESHERE.azurewebsites.net/api/checksomething?code=CODEGOESHERE</Item>
        <Item Key="SendClaimsIn">Body</Item>
        <Item Key="AuthenticationType">Basic</Item>
        <Item Key="AllowInsecureAuthInProduction">false</Item>
        <!-- 添加错误处理配置:指定API返回的错误字段映射 -->
        <Item Key="ErrorReasonClaimType">errorReason</Item>
        <Item Key="UserMessageClaimType">userMessage</Item>
      </Metadata>
      <CryptographicKeys>
        <Key Id="BasicAuthenticationUsername" StorageReferenceId="AUTHUSER" />
        <Key Id="BasicAuthenticationPassword" StorageReferenceId="AUTHPASS" />
      </CryptographicKeys>
      <InputClaims>
        <InputClaim ClaimTypeReferenceId="CLAIMTOSENDTOAPI" />
      </InputClaims>
      <OutputClaims>
        <!-- 定义从API返回的错误字段,用于展示给用户 -->
        <OutputClaim ClaimTypeReferenceId="userMessage" />
      </OutputClaims>
      <!-- 配置错误规则:当API返回400状态码时,触发页面错误 -->
      <OutputClaimsTransformations>
        <OutputClaimsTransformation ReferenceId="ReturnErrorOn400" />
      </OutputClaimsTransformations>
      <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
    </TechnicalProfile>
  </TechnicalProfiles>
</ClaimsProvider>

同时,需要确保你的外部API返回符合B2C要求的错误格式,比如当数据无效时返回:

{
  "version": "1.0.0",
  "status": 400,
  "userMessage": "你的员工账号已失效,请联系管理员处理"
}

2. 创建/修改自我断言技术配置,嵌入REST校验

找到你登录流程中使用的自我断言技术配置(比如SelfAsserted-LocalAccountSignin-Email),添加ValidationTechnicalProfiles节点,把REST校验作为其中一个验证步骤:

<ClaimsProvider>
  <DisplayName>Local Account</DisplayName>
  <TechnicalProfiles>
    <TechnicalProfile Id="SelfAsserted-LocalAccountSignin-Email">
      <DisplayName>Local Account Signin</DisplayName>
      <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.SelfAssertedAttributeProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      <!-- 保留原有的元数据、输入输出声明等配置 -->
      ...
      <!-- 添加REST校验作为验证步骤 -->
      <ValidationTechnicalProfiles>
        <!-- 保留原有的登录验证步骤(比如本地账号验证) -->
        <ValidationTechnicalProfile ReferenceId="login-NonInteractive" />
        <!-- 新增REST校验步骤,设置Precondition确保只有存在目标声明时才执行 -->
        <ValidationTechnicalProfile ReferenceId="REST-CheckInactiveEmployee">
          <Preconditions>
            <Precondition Type="ClaimsExist" ExecuteActionsIf="false">
              <Value>extension_empOPRID</Value>
              <Action>SkipThisValidationTechnicalProfile</Action>
            </Precondition>
          </Preconditions>
        </ValidationTechnicalProfile>
      </ValidationTechnicalProfiles>
    </TechnicalProfile>
  </TechnicalProfiles>
</ClaimsProvider>

3. 调整编排步骤,移除独立的REST校验步骤

删除原来单独的OrchestrationStep(Order=7的那个ClaimsExchange步骤),因为现在REST校验已经嵌入到登录的自我断言步骤里了,会在用户提交登录信息后自动执行。

原理说明

  • 单独的ClaimsExchange编排步骤属于后台无交互流程,校验失败时B2C会直接终止流程并把错误返回给应用;
  • 自我断言技术配置负责渲染用户交互页面(比如登录页),当它的ValidationTechnicalProfiles校验失败时,会把错误信息展示在当前页面,不会跳转回应用。

内容的提问来源于stack exchange,提问作者Andrew Wiebe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 07:25:33