重写Azure API管理基本身份验证时遇到问题

时间:2019-07-06 12:44:02

标签: azure azure-api-management

由于多种原因,我重写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>

MS文档

https://docs.microsoft.com/en-us/azure/api-management/api-management-policy-expressions

文档中存在方法“ TryParseBasic”。

1 个答案:

答案 0 :(得分:1)

谢谢@Thomas。 当我参考the JWT token实现它时,它就起作用了。

我使用ChandraShekhar Kaushik而不是TryParseBasic退休了。

AsBasic
相关问题