【发布时间】:2017-08-28 11:30:52
【问题描述】:
我非常希望能够根据用户的 Jwt 声明数据设置 Azure API 策略属性。我已经能够为计数器键和增量条件等设置字符串值,但我无法设置所有属性。我想像下面这样:
<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)"
/>
但是,当我保存策略时似乎会发生一些验证,因为我收到以下错误:
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.
我什至无法设置一个字符串参数(尽管格式很严格)
<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"])"
/>
当我尝试保存策略时,它会给出以下信息:
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) 等,但在保存时间似乎有一些验证正在停止它。
我认为将其添加到我的 API 中会是一个有用的功能,因此是否解决了这个问题?
【问题讨论】:
标签: jwt policy xml-attribute azure-api-management