由于多种原因,我重写the API Management Basic Authentication时, 我遇到以下错误,并退出了此修复程序。
有人知道正确的API管理政策吗? 谢谢。
Object.entries(localStorage)
<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>
https://docs.microsoft.com/en-us/azure/api-management/api-management-policy-expressions
文档中存在方法“ TryParseBasic”。
答案 0 :(得分:1)