azure - APIM 是否将相同的不记名 token 转发到后端 API? | OAuth 2.0 和 Azure AAD

标签 azure oauth-2.0 azure-active-directory azure-api-management apim

根据 Microsoft 提供的以下文档,我注册了这两个应用程序,使用客户端凭据设置 OAuth 2.0 服务,并添加了“validate-jwt”入站策略。我已经使用 postman 生成不记名 token 并在 APIM 实例下调用我的后端 API 并传递 token 来测试它。效果很好。

https://learn.microsoft.com/en-us/azure/api-management/api-management-howto-protect-backend-with-aad

但与 Apim 一样,我也想保护我的后端 API 并将相同的 token 传递给后端 API。所以我有一些问题 -

  • APIM 是否会自动将相同的不记名 token 转发到后端 API 还是我们需要为其配置任何策略?
  • 如果是这样,我如何检查跟踪日志?另外,我如何在后端 API 代码中授权相同的 token ?

这是我的“validate-jwt”政策 -

<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/{AAD Tenant ID}/v2.0/.well-known/openid-configuration" />
        <audiences>
            <audience>{App Id of backend App}</audience>
        </audiences>
    </validate-jwt>
    <base />
</inbound>

请帮忙。

最佳答案

对于你的第一个问题:

根据我这边的一些测试,APIM 似乎可以自动将相同的不记名 token 转发到后端 api,而无需添加任何策略。

我在APIM中创建了一个api来调用后端的microsoft graph api(列表用户)。测试运行APIM api,显示“401 Unauthorized”错误。然后我测试在 APIM api 的 header 中提供不记名 token ,如下图所示: enter image description here

它运行成功并响应用户列表。因此不记名 token 可以自动转发到后端API。

第二个问题:

如果你想跟踪后端API的日志,我认为你可以在你的API的后端代码中完成。

要验证后端 api 中的 token ,您可以解码后端 api 代码中的 jwt token ,然后检查 token 中声明的值(下面我提供了一个示例来解码 jwt token 并获取 的值国际空间站声明)

using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            var stream = "your access token";
            var handler = new JwtSecurityTokenHandler();
            var jsonToken = handler.ReadToken(stream);
            var tokenS = handler.ReadToken(stream) as JwtSecurityToken;

            var iss = tokenS.Claims.First(claim => claim.Type == "iss").Value;
            Console.WriteLine(iss);
            //Then check if "iss" matches the value you specified.

            Console.ReadLine();
        }
    }
}

关于azure - APIM 是否将相同的不记名 token 转发到后端 API? | OAuth 2.0 和 Azure AAD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66593657/

相关文章:

linux - 如何创建从 Azure Linux M 到 Azure 文件存储的符号链接(symbolic link)

android - 如何使用 OAuth2.0 解决 socialauth-android(by 3pillarlabs)和 Google Accounts/Google+

azure - 来自 Azure AD 的 JWT token 的签名验证失败

azure - OAuth 身份验证失败,并在 ios Xamarin 中出现错误 AADSTS65005

.net - 使用 Github 部署进行 Azure Secret Web.config 转换

oauth-2.0 - 使用 WSO2 IS 和 XACML 策略保护 REST API

javascript - 如何在通过 Oauth2 从 LinkedIn 获取访问 token 后获取用户个人资料数据

azure - 如何在 Azure 中增加角色可分配安全组

使用客户端凭据的 Azure AD 服务到服务调用 - 共享 key

azure - Azure 数据仓库中 sp_depends 的替代方案