使用自定义策略从JSON中提取布尔元素



我想问是否有一种方法可以从REST API的JSON响应中提取布尔元素。

我有一个包含JSON的声明:

{
"customerEntity": {
"role": {
"id": 1
}
},
"settings": {
"isAdmin": false,
"isBlocked": false
}
}

我已经尝试了以下claimstrtransforms: GetClaimFromJson, GetSingleItemFromJson但是它在上传到身份体验框架期间返回一个错误,因为预期提取的索赔是一个字符串。

非常感谢您的帮助。

最简单的解决方案是将响应映射到来自REST API技术概要文件的声明。

我将添加一个用于测试 的简单示例。技术概要文件

<TechnicalProfile Id="TestEchoJson">
<DisplayName>Test Echo</DisplayName>
<Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.RestfulProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Metadata>
<Item Key="ServiceUrl">{Settings:TestApiUrl}</Item>
<Item Key="AuthenticationType">None</Item>
<Item Key="ResolveJsonPathsInJsonTokens">true</Item>
<Item Key="SendClaimsIn">QueryString</Item>
</Metadata>
<OutputClaims>
<OutputClaim ClaimTypeReferenceId="isAdmin" PartnerClaimType="settings.isAdmin" />
<OutputClaim ClaimTypeReferenceId="customProperty" PartnerClaimType="settings.customProperty" />
</OutputClaims>
</TechnicalProfile>

Claim模式

<ClaimType Id="customProperty">
<DisplayName>customProperty</DisplayName>
<DataType>string</DataType>
</ClaimType>

<ClaimType Id="isAdmin">
<DisplayName>isAdmin</DisplayName>
<DataType>boolean</DataType>
</ClaimType>

用于测试的模拟Api

app.get('/echo', async (req, res) => {
let jsonResponse = {
"customerEntity": {
"role": {
"id": 1
}
},
"settings": {
"isAdmin": false,
"isBlocked": false,
"customProperty": "Hello there"
},
};
res.json(jsonResponse);
});

最新更新