B2C自定义策略中RestAPI错误响应的本地化消息

时间:2020-01-17 14:02:54

标签: azure-ad-b2c

我在自定义策略中进行了本地化,但是在某些步骤中,我正在调用REST API来验证一些数据。响应以英语进行,但是现在我也需要翻译该消息,作为本地化的一部分。在B2C中有什么方法可以做到这一点?

这是我从API得到的响应:

                {
                    "userMessage": "Password is incorrect",
                    "version":"1.0.0",
                    "status:: 409,
                    "code": "API12345",
                    "requestId":"50f0bd91-2ff4-4b8f-828f-00f170519ddb",
                    "developerMessage":"Verbose description of problem and how to fix it.",
                    "moreInfo": "https://restapi/error/API12345/moreinfo"
                }

1 个答案:

答案 0 :(得分:1)

您可以将本地化参数发送到REST API,并使其返回本地化错误。或者,您可以从API返回错误代码,而不是错误字符串。然后使用以下示例在策略中完成此操作:

    <BuildingBlocks>
        <ClaimsSchema>
            <ClaimType Id="errorCode">
                <DisplayName>errorCode</DisplayName>
                <DataType>string</DataType>
                <UserHelpText>A claim responsible for holding response codes to send to the relying party</UserHelpText>
            </ClaimType>
            <ClaimType Id="messageValue">
                <DisplayName>Message</DisplayName>
                <DataType>string</DataType>
                <UserHelpText>A claim responsible for holding response messages to send to the relying party</UserHelpText>
                <UserInputType>Paragraph</UserInputType>
                <Restriction>
                    <Enumeration Text="errorCode1" Value="will get overidden by localization" />
                    <Enumeration Text="errorCode2" Value="will get overidden by localization" />
                </Restriction>
            </ClaimType>
        </ClaimsSchema>
        <ClaimsTransformations>
            <ClaimsTransformation Id="SetMessageId" TransformationMethod="CreateStringClaim">
                <InputParameters>
                    <InputParameter Id="value" DataType="string" Value="errorCode1" /> <!-- Toggle for errorCode2 -->
                </InputParameters>
                <OutputClaims>
                    <OutputClaim ClaimTypeReferenceId="errorCode" TransformationClaimType="createdClaim" />
                </OutputClaims>
            </ClaimsTransformation>
            <ClaimsTransformation Id="GetLocalizedMessage" TransformationMethod="GetMappedValueFromLocalizedCollection">
                <InputClaims>
                    <InputClaim ClaimTypeReferenceId="errorCode" TransformationClaimType="mapFromClaim" />
                </InputClaims>
                <OutputClaims>
                    <OutputClaim ClaimTypeReferenceId="messageValue" TransformationClaimType="restrictionValueClaim" />
                </OutputClaims>
            </ClaimsTransformation>
        </ClaimsTransformations>
        <ContentDefinitions>
            <ContentDefinition Id="api.selfasserted">
                <LocalizedResourcesReferences MergeBehavior="Prepend">
                    <LocalizedResourcesReference Language="en" LocalizedResourcesReferenceId="api.selfasserted.en" />
                    <LocalizedResourcesReference Language="es" LocalizedResourcesReferenceId="api.selfasserted.es" />
                </LocalizedResourcesReferences>
            </ContentDefinition>
        </ContentDefinitions>
        <Localization Enabled="true">
            <SupportedLanguages DefaultLanguage="en" MergeBehavior="ReplaceAll">
                <SupportedLanguage>en</SupportedLanguage>
                <SupportedLanguage>es</SupportedLanguage>
            </SupportedLanguages>
            <LocalizedResources Id="api.selfasserted.en">
                <LocalizedCollections>
                    <LocalizedCollection ElementType="ClaimType" ElementId="messageValue" TargetCollection="Restriction">
                        <Item Text="errorCode1" Value="First message in english" />
                        <Item Text="errorCode2" Value="Second message in english" />
                    </LocalizedCollection>
                </LocalizedCollections>
            </LocalizedResources>
            <LocalizedResources Id="api.selfasserted.es">
                <LocalizedCollections>
                    <LocalizedCollection ElementType="ClaimType" ElementId="messageValue" TargetCollection="Restriction">
                        <Item Text="errorCode1" Value="Primer mensaje en español" />
                        <Item Text="errorCode2" Value="Segundo mensaje en español" />
                    </LocalizedCollection>
                </LocalizedCollections>
            </LocalizedResources>
        </Localization>
    </BuildingBlocks>
    <ClaimsProviders>
        <ClaimsProvider>
            <DisplayName>Self Asserted</DisplayName>
            <TechnicalProfiles>
                <TechnicalProfile Id="SelfAsserted-WelcomePage">
                    <DisplayName>User profile</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>
                    </Metadata>
                    <InputClaimsTransformations>
                        <InputClaimsTransformation ReferenceId="SetMessageId" />
                        <InputClaimsTransformation ReferenceId="GetLocalizedMessage" />
                    </InputClaimsTransformations>
                    <InputClaims>
                        <InputClaim ClaimTypeReferenceId="messageValue" />
                    </InputClaims>
                    <OutputClaims>
                        <OutputClaim ClaimTypeReferenceId="email"/>
                        <OutputClaim ClaimTypeReferenceId="messageValue"/>
                    </OutputClaims>
                </TechnicalProfile>
            </TechnicalProfiles>
        </ClaimsProvider>
    </ClaimsProviders>
    <UserJourneys>
        <UserJourney Id="Localization_Tester">
            <OrchestrationSteps>
                <OrchestrationStep Order="1" Type="ClaimsExchange">
                    <ClaimsExchanges>
                        <ClaimsExchange Id="SelfAsserted-WelcomePage" TechnicalProfileReferenceId="SelfAsserted-WelcomePage" />
                    </ClaimsExchanges>
                </OrchestrationStep>
                <OrchestrationStep Order="2" Type="ClaimsExchange">
                    <ClaimsExchanges>
                        <ClaimsExchange Id="AAD-UserReadUsingEmailAddress" TechnicalProfileReferenceId="AAD-UserReadUsingEmailAddress" />
                    </ClaimsExchanges>
                </OrchestrationStep>
                <OrchestrationStep Order="3" Type="SendClaims" CpimIssuerTechnicalProfileReferenceId="JwtIssuer" />
            </OrchestrationSteps>
        </UserJourney>
    </UserJourneys>
相关问题