Localised message for RestAPI error response in B2C custom policy

后端 未结 1 1714
囚心锁ツ
囚心锁ツ 2021-01-28 07:42

I did localisation in my custom policy, but in certain steps I\'m calling REST API to validate some data. Response is coming in English, but now I need to translate that message

相关标签:
1条回答
  • 2021-01-28 08:19

    You can send the localisation parameter to the REST API and have it return a localised error. Or you can return back an error code from the API instead of an error string. Then use the following example to have this done in policy:

        <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>
    
    0 讨论(0)
提交回复
热议问题