【发布时间】:2019-11-16 17:53:33
【问题描述】:
当我出于几个原因重写the API Management Basic Authentication时, 我遇到了以下错误并停止了修复。
有人知道正确的 API 管理政策吗? 谢谢。
政策
<policies>
<inbound>
<set-variable name="isAuthOk"
value="@{
string[] value;
BasicAuthCredentials credit;
if (context.Request.Headers.TryGetValue("Authorization", out value))
{
if(TryParseBasic(value[0], out credit)){
if(credit.UserId == "nelco1"){
return true;
}else{
return false;
}
}
}
else
{
return false;
}
}" />
<base />
<!-- thankx for https://stackoverflow.com/questions/49479416/api-management-basic-authentication -->
<choose>
<when condition="@(context.Variables.GetValueOrDefault<bool>("isAuthOk"))">
</when>
<otherwise>
<return-response>
<set-status code="401" reason="Unauthorized" />
<set-header name="WWW-Authenticate" exists-action="override">
<value>Basic realm="ohhhhhhhhh"</value>
</set-header>
<set-body>Wrong username or password</set-body>
</return-response>
</otherwise>
</choose>
<set-backend-service id="apim-generated-policy" backend-id="preaddresscode2" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
我保存上述策略时出现错误
One or more fields contain incorrect values:
Error in element 'set-variable' on line 3, column 10: The name 'TryParseBasic' does not exist in the current context
微软文档
https://docs.microsoft.com/en-us/azure/api-management/api-management-policy-expressions
文档中存在方法“TryParseBasic”。
【问题讨论】:
-
TryParseBasic是什么?如错误中所述,此方法不存在 -
@Thomas 谢谢你的提问。请检查官方文档。该方法存在于那里。你为什么说这个问题? docs.microsoft.com/en-us/azure/api-management/…
-
你能试试
value[0].TryParseBasic(out credit)这样的东西吗?因为TryParseBasic是一个扩展方法。 -
@Thomas 我还有一个错误。第 5 行第 18 列的元素“set-variable”出错:方法“TryParseBasic”没有重载需要 2 个参数。
-
你能发布你的更新代码吗?
标签: azure azure-api-management