简体   繁体   中英

Iterate a array claim in azure API management Policy

I want to iterate a claim of JWT token in azure api management policy

<set-variable name="testparams" value="@(context.Request.Headers["Authorization"].First().Split(' ')[1].AsJwt()?.Claims["carlist"].FirstOrDefault())" />

<set-variable name="values">
        {% for value in testparams %}
            if(value=="BMW")
               <set-variable name="testparams" value=true />
               break
            else
            <set-variable name="testparams" value=false />
        {% endfor %}
    />
        <base />
    </inbound>
<policies>

this is the way i want to iterate and set a variable if it is success. when i put this code in azure policy it shows syntax error.how to achieve this?

claim is

carlist = "[BMW,AUDI,FIAT]"

There are some mistakes in your code.

First, .FirstOrDefault() is to get the first item of a list. According to my test, it will get " BMW " but not " [BMW,AUDI,FIAT] ".

Then, if we use <set-variable name="testparams" value="@(context.Request.Headers["Authorization"].First().Split(' ')[1].AsJwt()?.Claims["carlist"])" /> without .FirstOrDefault() . It will show error message because the variable can't be set as array in <set-variable> tag.

Apart from this, in your next code I'm confused about how do you want to set a variable. I don't know if you want to set the value true/false to variable testparams or to variable values . And there are some syntax errors in the code.

According to some test, I think it is difficult to get a array list from the jwt token and set it as variable in <set-variable> tag. I provide a workaround for your reference which can set a variable resultVar in code in <set-body> . Please check if it can meet your requirement.

<inbound>
    <base />
    <set-body>@{
        string[] carList = context.Request.Headers["Authorization"].First().Split(' ')[1].AsJwt()?.Claims["carlist"];
        string resultVar = "";
        foreach(string value in carList)
        {
            if (value == "BMW") {
                resultVar = "true";
                break;
            }else {
                resultVar = "false";
            }
        }
        return resultVar;
    }</set-body>
</inbound>

Or you could just simplify your set-body like this

<inbound>
   <base />
   <set-body>@{
       string[] carList = context.Request.Headers["Authorization"].First().Split(' ')[1].AsJwt()?.Claims["carlist"];
       return carList.Contains("BMW").ToString();
  }
   </set-body>
</inbound>

If anyone is still looking for answers to loop inside set-variable when the input is of type json, this could help

<set-variable name="values" value="@{
            JObject json = (JObject)context.Variables["testparams"];
 var array = (json)["carlist"];       
var ret = (string)"";//set some default string or bool data;
           foreach (var value in array)
                    {
            if ((string)value=="BMW")
            {
             ret = "true";
               break;
               }
            else
            {
            ret = "false";
            }
            }
            return ret;
            }" />

(I used only 'If' for one of the scenarios I worked on. Haven't really tried a ''else'.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM