自定义身份验证的入站策略问题

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

我在入站策略中的自定义身份验证

when
条件下遇到问题,我尝试了各种方法来找出,我无法识别,发送请求内的API和发送请求代码本身工作正常。

<send-request mode="new" response-variable-name="authResponse" timeout="60" ignore-error="false">
            <set-url>@("my_url")</set-url>
            <set-method>GET</set-method>
            <set-header name="Authorization" exists-action="override">
                <value>@("Bearer " + context.Request.Headers.GetValueOrDefault("Authorization", ""))</value>
            </set-header>
</send-request>
<choose>
            <when condition="@((int)((Newtonsoft.Json.Linq.JObject)context.Variables["authResponse"]).Property("StatusCode").Value == 200)">
                <!-- Continue with the API call -->
            </when>
            <otherwise>
                <return-response>
                    <set-status code="401" reason="Unauthorized" />
                    <set-header name="Content-Type" exists-action="override">
                        <value>application/json</value>
                    </set-header>
                    <set-body>
                        {
                            "errorMessage": "Authentication failed"
                        }
                    </set-body>
                </return-response>
            </otherwise>

</choose>

我也尝试过这个,但这不起作用

<when condition="@((int)context.Variables["authResponse.StatusCode"] == 200)">
                <!-- Continue with the API call -->
</when>

我收到的回复是这样的,API 没有问题

{
    "statusCode": 500,
    "message": "Internal server error",
    "activityId": "91c498c2-a213-4f38-bb38-494c331bc46e"
}

azure-web-app-service azure-api-management
1个回答
0
投票

您可以使用以下策略来设置状态代码的

when condition

<inbound >
    <base />
    <send-request mode="new" response-variable-name="authResponse" timeout="60" ignore-error="false">
        <set-url>@("my_url")</set-url>
        <set-method>GET</set-method>
        <set-header name="Authorization" exists-action="override">
            <value>@("Bearer " + context.Request.Headers.GetValueOrDefault("Authorization", ""))</value>
        </set-header>
    </send-request>
    <choose>
        <when condition="@(((IResponse)context.Variables["authResponse"]).StatusCode==200)>
            <!-- Added response for Testing -->
            <return-response>
                <set-body>Successfully Authenticated...</set-body>
            </return-response>
        </when>
        <otherwise>
            <return-response>
                <set-status code="401" reason="Unauthorized" />
                <set-header name="Content-Type" exists-action="override">
                    <value>application/json</value>
                </set-header>
                <set-body>Authentication Failed...</set-body>
            </return-response>
        </otherwise>
    </choose>
</inbound>

通过使用

<when condition="@(((IResponse)context.Variables["authResponse"]).StatusCode==200)>
,我能够得到预期的响应,如下所示。

enter image description here

痕迹-

enter image description here

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