在 Azure API 管理 Oauth2 服务中指定受众参数

Specify audience parameter in Azure API Management Oauth2 Service

我正在尝试在 Azure API 管理中设置 Oauth2 身份验证服务,以便在开发人员门户中的 Auth0 身份提供程序中对用户进行身份验证。
但是我无法将 Oauth2 服务配置为传递受众参数以获取 JWT 令牌(现在仅返回不透明令牌)。

我在 Azure 门户中创建了一个新的 Oath2 服务,在“其他主体参数”部分指定了受众:

接下来,我将 Oath2 服务添加到 API:

接下来,当我尝试在开发人员门户中测试 API 时,我只得到一个不透明令牌:

我希望在“其他参数”部分中指定时会包括受众,但这似乎不起作用。
所以我想知道这是不是我做错了什么。

您可以配置 jwt 验证器策略 以使用 reference/opaque 令牌。

政策声明

<inbound>
  <!-- Extract Token from Authorization header parameter -->
  <set-variable name="token" value="@(context.Request.Headers.GetValueOrDefault("Authorization","scheme param").Split(' ').Last())" />

  <!-- Send request to Token Server to validate token (see RFC 7662) -->
  <send-request mode="new" response-variable-name="tokenstate" timeout="20" ignore-error="true">
    <set-url>https://microsoft-apiappec990ad4c76641c6aea22f566efc5a4e.azurewebsites.net/introspection</set-url>
    <set-method>POST</set-method>
    <set-header name="Authorization" exists-action="override">
      <value>basic dXNlcm5hbWU6cGFzc3dvcmQ=</value>
    </set-header>
    <set-header name="Content-Type" exists-action="override">
      <value>application/x-www-form-urlencoded</value>
    </set-header>
    <set-body>@($"token={(string)context.Variables["token"]}")</set-body>
  </send-request>

  <choose>
        <!-- Check active property in response -->
        <when condition="@((bool)((IResponse)context.Variables["tokenstate"]).Body.As<JObject>()["active"] == false)">
            <!-- Return 401 Unauthorized with http-problem payload -->
            <return-response>
                <set-status code="401" reason="Unauthorized" />
                <set-header name="WWW-Authenticate" exists-action="override">
                    <value>Bearer error="invalid_token"</value>
                </set-header>
            </return-response>
        </when>
    </choose>
  <base />
</inbound>

您可以在 https://jwt.io/ 解码令牌并使用入站部分中使用的 validate-jwt 策略重新验证它: 例如:

解码后的令牌负载中的受众应该与 validate-jwt 策略的声明部分相匹配:

您可以在 Jwt validation policy with reference tokens?, Verify a reference token with an authorization server, and Protect API's using OAuth 2.0 in APIM

参考打开的 GitHub issue

我们解决这个问题的方法是在 Auth0 的“设置”下设置“默认受众”:

这样,当请求中没有提供受众时,Auth0 将使用默认受众作为后备。