Azure API 管理:Oauth2 401 授权给 "Unauthorized. Access token is missing or invalid."
Azure API Management: authorization with Oauth2 401 gives "Unauthorized. Access token is missing or invalid."
我想通过 Azure API 管理公开一些 APIs(逻辑应用程序、功能)。
它们工作正常,所以我决定添加 OAuth2 授权。
我一步步跟着https://docs.microsoft.com/fr-fr/azure/api-management/api-management-howto-protect-backend-with-aad:
- 在 Azure AD 中注册一个应用程序 (backend-app) 来表示 API。
- 在 Azure AD 中注册另一个应用程序 (client-app) 以表示需要调用 API 的客户端应用程序。
- 在 Azure AD 中,授予允许客户端应用程序调用后端应用程序的权限。
- 配置开发人员控制台以使用 OAuth 2.0 用户授权调用 API。
- 添加 validate-jwt 策略来验证每个传入请求的 OAuth 令牌。
- 同样使用Postman进行测试
一切正常,直到 "validate-jwt" 策略步骤。
当我添加它时,我收到“401 - 未经授权。访问令牌丢失或无效。”
我可以在 Developer Console 和 Postman 中获取令牌,但是一旦我进行 API 调用... 401!
当我使用 jwt.ms 检查令牌的内容时,我注意到 aud 参数与后端应用程序 ID 无关。
令牌中的值为“00000003-0000-0000-c000-000000000000”,而后端应用 ID 类似于“16caXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXc0”。
我 运行 没有想法,需要一些 Azure 专家的帮助!
非常感谢帮助...
下面是根据 MS 文档的入站政策:
<policies>
<inbound>
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url="https://login.microsoftonline.com/MY_AD_TENANT_ID/.well-known/openid-configuration" />
<required-claims>
<claim name="aud">
<value>MY8BACKEND_APP_ID_GUID</value>
</claim>
</required-claims>
</validate-jwt>
</inbound>
<backend>
<forward-request />
</backend>
<outbound />
<on-error />
</policies>
Screen cap of the Postman screen where I get the token (this works, but then when I send the request --> 401)
Screen cap of aud param in jwt.ms
您并不是真的需要检查 aud 参数的值。您可以一起删除 required-claims
,这样仍然可以验证令牌存在和签名。如果您想确保为您的应用程序颁发了令牌,只需找到包含应用程序 ID 的声明并在 name="..." 中使用它来匹配您的应用程序 ID 值。
几年前我在验证 Azure AD 令牌时遇到了一些问题 - 请参阅我的 write up。
我怀疑问题是 JWT 中的随机数 header。
如果使用 v2 版本端点,转到 -> Azure 广告 -> 应用注册 -> select 后端应用 -> 清单 -> 更新 属性 "accessTokenAcceptedVersion":2, "
我想通过 Azure API 管理公开一些 APIs(逻辑应用程序、功能)。 它们工作正常,所以我决定添加 OAuth2 授权。
我一步步跟着https://docs.microsoft.com/fr-fr/azure/api-management/api-management-howto-protect-backend-with-aad:
- 在 Azure AD 中注册一个应用程序 (backend-app) 来表示 API。
- 在 Azure AD 中注册另一个应用程序 (client-app) 以表示需要调用 API 的客户端应用程序。
- 在 Azure AD 中,授予允许客户端应用程序调用后端应用程序的权限。
- 配置开发人员控制台以使用 OAuth 2.0 用户授权调用 API。
- 添加 validate-jwt 策略来验证每个传入请求的 OAuth 令牌。
- 同样使用Postman进行测试
一切正常,直到 "validate-jwt" 策略步骤。 当我添加它时,我收到“401 - 未经授权。访问令牌丢失或无效。” 我可以在 Developer Console 和 Postman 中获取令牌,但是一旦我进行 API 调用... 401!
当我使用 jwt.ms 检查令牌的内容时,我注意到 aud 参数与后端应用程序 ID 无关。 令牌中的值为“00000003-0000-0000-c000-000000000000”,而后端应用 ID 类似于“16caXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXc0”。
我 运行 没有想法,需要一些 Azure 专家的帮助! 非常感谢帮助...
下面是根据 MS 文档的入站政策:
<policies>
<inbound>
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url="https://login.microsoftonline.com/MY_AD_TENANT_ID/.well-known/openid-configuration" />
<required-claims>
<claim name="aud">
<value>MY8BACKEND_APP_ID_GUID</value>
</claim>
</required-claims>
</validate-jwt>
</inbound>
<backend>
<forward-request />
</backend>
<outbound />
<on-error />
</policies>
Screen cap of the Postman screen where I get the token (this works, but then when I send the request --> 401)
Screen cap of aud param in jwt.ms
您并不是真的需要检查 aud 参数的值。您可以一起删除 required-claims
,这样仍然可以验证令牌存在和签名。如果您想确保为您的应用程序颁发了令牌,只需找到包含应用程序 ID 的声明并在 name="..." 中使用它来匹配您的应用程序 ID 值。
几年前我在验证 Azure AD 令牌时遇到了一些问题 - 请参阅我的 write up。
我怀疑问题是 JWT 中的随机数 header。
如果使用 v2 版本端点,转到 -> Azure 广告 -> 应用注册 -> select 后端应用 -> 清单 -> 更新 属性 "accessTokenAcceptedVersion":2, "