【问题标题】:Trouble with Rewriting Azure API Management Basic Authentication重写 Azure API 管理基本身份验证的问题
【发布时间】: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


【解决方案1】:

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

我退休时使用 TryParseBasic 而不是 AsBasic

<policies>
    <inbound>
        <set-backend-service id="apim-generated-policy" backend-id="preaddresscode2" />
        <rewrite-uri template="/HttpTrigger1" />
        <set-variable name="isAuthOk" value="@{
    string[] value;
  if (context.Request.Headers.TryGetValue("Authorization", out value))
  {
    BasicAuthCredentials credit = context.Request.Headers.GetValueOrDefault("Authorization","").AsBasic();
    if(credit == null){
        return false;
    }
    switch(credit.UserId){
        case "UUUUUU1":
            // it seems an ugly implementation.
            if(credit.Password.Equals("PPPPPP1")){
                return true;
            }
        case "UUUUUU2":
            if(credit.Password.Equals("PPPPPP2")){
                return true;
            }
        break;
            default:
        break;        
    }
    return false;
  }
  else
  {
    return false;
  }
  return true;
        }" />
        <base />
        <choose>
            <when condition="@(context.Variables.GetValueOrDefault<bool>("isAuthOk"))" />
            <otherwise>
                <return-response>
                    <set-status code="401" reason="Unauthorized" />
                    <set-header name="WWW-Authenticate" exists-action="override">
                        <value>Basic realm="someRealm"</value>
                    </set-header>
                    <set-body>Wrong username or password</set-body>
                </return-response>
            </otherwise>
        </choose>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 2021-01-18
    • 2023-04-01
    • 2018-06-21
    相关资源
    最近更新 更多