Azure APIM策略编辑器 [英] Azure APIM Policy Editor
问题描述
我非常希望能够基于用户的Jwt Claims数据设置Azure API策略属性.我已经能够为计数器键和递增条件设置字符串值,但是我无法设置所有属性.我想过要做类似以下的事情:
I would very much like to be able to set Azure API Policy attributes based on a User's Jwt Claims data. I have been able to set string values for things like the counter-key and increment-condition but I can't set all attributes. I imagined doing something like the following:
<rate-limit-by-key
calls="@((int) context.Variables["IdentityToken"].AsJwt().Claims.GetValueOrDefault("/LimitRate/Limit", "5"))"
renewal-period="@((int) context.Variables["IdentityToken"].AsJwt().Claims.GetValueOrDefault("/LimitRate/Duration/InSeconds", "60"))"
counter-key="@((string)context.Variables["Subject"])"
increment-condition="@(context.Response.StatusCode == 200)"
/>
但是,当我收到以下错误消息时,在保存策略时似乎正在进行一些验证:
However there seems to be some validation happening when I save the policy as I get the following error:
Error in element 'rate-limit-by-key' on line 98, column 10: The 'calls' attribute is invalid - The value '@((int) context.Variables["IdentityToken"].AsJwt().Claims.GetValueOrDefault("/LimitRate/Limit", "5"))' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:int' - The string '@((int) context.Variables["IdentityToken"].AsJwt().Claims.GetValueOrDefault("/LimitRate/Limit", "5"))' is not a valid Int32 value.
我什至在设置字符串参数时都遇到麻烦(尽管格式很严格)
I even have trouble setting a string parameter (albeit one with a strict format)
<quota-by-key
calls="10"
bandwidth="100"
renewal-period="@((string) context.Variables["IdentityToken"].AsJwt().Claims.GetValueOrDefault("/Quota/RenewalPeriod", "P00Y00M01DT00H00M00S"))"
counter-key="@((string)context.Variables["Subject"])"
/>
当我尝试保存该策略时,会给出以下内容:
Which gives the following when I try and save the policy:
Error in element 'quota-by-key' on line 99, column 6: @((string) context.Variables["IdentityToken"].AsJwt().Claims.GetValueOrDefault("/Quota/RenewalPeriod", "P00Y00M01DT00H00M00S")) is not in a valid format. Provide number of seconds or use 'PxYxMxDTxHxMxS' format where 'x' is a number.
我尝试了大量的变体转换,Convert.ToInt32,声称不是字符串,@ {return 5},@(5)等,但是似乎在节省时间发生了一些验证,正在停止它.
I have tried a large set of variations casting, Convert.ToInt32, claims that are not strings, @{return 5}, @(5) etc but there seems to be some validation happening at save time that is stopping it.
是否存在这个问题,因为我认为添加到我的API中将是一个有用的功能吗?
Is there away around this issue as I think it would be a useful feature to add to my API?
推荐答案
rate-limit-by-key和quota-by-key上的calls属性不支持策略表达式.内部限制使我们无法根据每个请求对其进行处理.最好的办法是将请求分类为几个有限的组,并使用选择策略有条件地应用速率限制/配额.
calls attribute on rate-limit-by-key and quota-by-key does not support policy expressions. Internal limitations block us from treating it on per-request basis unfortunately. The best you can do is categorize requests into a few finite groups and apply rate limit/quota conditionally using choose policy.
或者尝试使用递增计数属性来控制每个请求增加多少计数器.
Or try using increment-count attribute to control by how much counter is increased per each request.
这篇关于Azure APIM策略编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!