在编排步骤上运行验证技术配置文件,并在验证失败时向用户显示错误

问题描述 投票:0回答:1

我制定了注册和登录的自定义策略。在后续登录期间,我需要对外部 API 执行检查,看看注册时收集的数据是否有效。如果它无效,我需要向用户显示错误。

我已经创建了外部 API,可以从策略中调用它。但是,我不确定如何在登录期间执行此检查,因为它不是自定义的自断言技术配置文件。

我尝试直接添加 RestfulProvider 技术配置文件作为用户旅程中的编排步骤。虽然这确实调用了我的 API 并执行检查,但我的问题是,它不会在登录屏幕上显示错误,它会使用从我的 API 返回的消息的 error_description 参数重定向到我的应用程序。

我希望能够在登录屏幕或另一个 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>
            <!-- Set the ServiceUrl with your own REST API endpoint -->
            <Item Key="ServiceUrl">https://NAMEOFFUNCTIONGOESHERE.azurewebsites.net/api/checksomething?code=CODEGOESHERE</Item>
            <!-- Claims will be sent in the body as raw json. -->
            <Item Key="SendClaimsIn">Body</Item>
            <!-- Set AuthenticationType to Basic. -->
            <Item Key="AuthenticationType">Basic</Item>
            <!-- REMOVE the following line in production environments -->
            <Item Key="AllowInsecureAuthInProduction">false</Item>
          </Metadata>
          <CryptographicKeys>
            <!-- Username and pass to be used for basic auth with the API. -->
            <Key Id="BasicAuthenticationUsername" StorageReferenceId="AUTHUSER" />
            <Key Id="BasicAuthenticationPassword" StorageReferenceId="AUTHPASS" />
          </CryptographicKeys>
          <InputClaims>
            <!-- Claims sent to your REST API -->
            <InputClaim ClaimTypeReferenceId="CLAIMTOSENDTOAPI" />
          </InputClaims>
          <OutputClaims>
            <!-- Claims parsed from your REST API -->
          </OutputClaims>
          <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
        </TechnicalProfile>
      </TechnicalProfiles>
    </ClaimsProvider>

我的编排步骤。

<OrchestrationStep Order="7" Type="ClaimsExchange">
          <Preconditions>
            <Precondition Type="ClaimsExist" ExecuteActionsIf="false">
              <Value>extension_empOPRID</Value>
              <Action>SkipThisOrchestrationStep</Action>
            </Precondition>
          </Preconditions>
          <ClaimsExchanges>
            <ClaimsExchange Id="REST-CheckInactiveEmployee" TechnicalProfileReferenceId="REST-CheckInactiveEmployee" />
          </ClaimsExchanges>
        </OrchestrationStep>
azure-ad-b2c azure-ad-b2c-custom-policy
1个回答
0
投票

当调用返回 HTTP 错误代码时(如您所见),作为其自己的编排步骤的 REST API 调用会向重定向 URL 返回异常。

但是,您通常可以将验证配置文件添加到任何自断言配置文件中,因此您可以将 REST API 调用作为

ValidationTechnicalProfile
添加到与登录选项关联的自断言配置文件中。

例如,在 MS ADB2C 自定义策略入门包的组合登录和注册旅程中,第一个编排步骤定义如下:

<OrchestrationStep Order="1" Type="CombinedSignInAndSignUp" ContentDefinitionReferenceId="api.signuporsignin">
    <ClaimsProviderSelections>
       <ClaimsProviderSelection TargetClaimsExchangeId="FacebookExchange" />
       <ClaimsProviderSelection ValidationClaimsExchangeId="LocalAccountSigninEmailExchange" />
    </ClaimsProviderSelections>
    <ClaimsExchanges>
       <ClaimsExchange Id="LocalAccountSigninEmailExchange" TechnicalProfileReferenceId="SelfAsserted-LocalAccountSignin-Email" />
    </ClaimsExchanges>
</OrchestrationStep>

您可以看到用于处理登录字段的技术配置文件 ID 是

SelfAsserted-LocalAccountSignin-Email
。如果您在同一策略中检查此配置文件,则它是自断言配置文件,并且登录时的正常用户名/密码检查是在其上实现为
ValidationTechnicalProfile
的。假设您正在基于 starterpack 创建策略,您可以将 API 调用配置文件添加为
ValidationTechnicalProfile
自断言配置文件上的另一个
SelfAsserted-LocalAccountSignin-Email
,并且当用户尝试使用表单登录时,您的配置文件也会得到验证在登录页面上。例如:

<ValidationTechnicalProfiles>
  <ValidationTechnicalProfile ReferenceId="login-NonInteractive" />
  <ValidationTechnicalProfile ReferenceId="REST-CheckInactiveEmployee" />
</ValidationTechnicalProfiles>

(配置文件的顺序就是它们被调用的顺序,因此您应该根据您是否希望首先验证用户名/密码有效性或您的 API 调用来对它们进行排序)。

此外,为了让 B2C 显示您选择的错误消息(当用于验证任何自断言的配置文件时,而不仅仅是用于登录页面的配置文件时),您的 REST API 将需要返回适当的响应 -

 4xx
HTTP 响应(例如
409
错误),包含包含以下字段的 JSON 对象:

{
  "version": "1.0.0",
  "status": 409,
  "userMessage": "An error occurred in validation."
}

其中

version
包含字符串格式的 API 版本号(可以是您认为合适的任何版本号),
status
包含您的 HTTP 状态代码作为整数(文档说它必须是 409,但我不知道实际情况是否如此,或者是否应该与响应的状态代码匹配),并且
userMessage
包含您希望 B2C 在表单上显示的消息以响应错误。

© www.soinside.com 2019 - 2024. All rights reserved.