Search code examples
azure-web-app-serviceazure-api-management

Issue in inbound policy for custom authentication


I have issue in when condition of this custom authentication in inbound policy, I tried various method to find out, I couldn't able to identify, API inside the send-request and send-request code itself working fine.

<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>

I tried this as well, but this not working

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

Response I recieve is this, the API as no issue

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


Solution

  • You can use the below policy to set the when condition for status code.

    <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>
    

    By using <when condition="@(((IResponse)context.Variables["authResponse"]).StatusCode==200)>, I am able to get the expected response as shown below.

    enter image description here

    Trace-

    enter image description here