Is it possible to create a policy that will conditionally expose an api management endpoint in azure even when a opim-subscription key is required?

不问归期 提交于 2021-02-11 14:02:43

问题


An example of what I am looking for is as follows but the allow-access element does not exist. What can I replace with so that the subscription key is not checked. i.e. in this case it would allow all callers access to the controller as long as they are making GET requests.

<policies>
<inbound>
    <base />
    <choose>
        <when condition="@(context.Request.Method.Equals("GET"))">
            <allow-access />
        </when>
    </choose>
</inbound>
<backend>
    <base />
</backend>
<outbound>
    <base />
</outbound>
<on-error>
    <base />
</on-error>

Any help would be appreciated.


回答1:


A workaround would be to turn off the Requires subscription setting on the product and check the subscription key in the inbound policy by yourself. Here is an example of how to do it.

  1. Go to Settings of Starter product.
  2. Uncheck Requires subscription and save.
  3. Open the policies of the product and add the following policy to the inbound. The value of <check-header> policy is the subscription key of the Starter product.
<choose>
    <when condition="@(!context.Request.Method.Equals("GET"))">
        <check-header name="Ocp-Apim-Subscription-Key" failed-check-httpcode="401" failed-check-error-message="Not authorized" ignore-case="false">
            <value>920b4e307f4f41ff9bd4a3bd6a5450ee</value>
        </check-header>
    </when>
</choose>


来源:https://stackoverflow.com/questions/62900564/is-it-possible-to-create-a-policy-that-will-conditionally-expose-an-api-manageme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!