验证网络中 B2C 访问令牌的范围 API

Validate scopes of B2C access token in an web API

我们有两个独立的 dotnet 核心 apis(API1 和 API2),它们使用 azure ad b2c 进行保护。这两个 api 都在 b2c 租户上注册并公开了它们的范围。我们有一个客户端 Web 应用程序可以访问上述受保护的 apis。此 Web 应用已在 b2c 租户中注册,并为上述 api 设置了 api 权限,并公开了适当的范围。

关于什么是配置 Web 应用程序的最佳方法,以便它能够访问多个受保护的 apis,建议采用一种方法“将两种服务合并到一个应用程序注册中并公开不同的范围。"

在尝试实现这一点的同时,我还将验证访问令牌中存在的范围以及受众和权限。

public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(jwtOptions =>
            {
                //validating the access token with client id and token issuer(authority)
                jwtOptions.Authority = Configuration["AzureAdB2C:Authority"];
                jwtOptions.Audience = Configuration["AzureAdB2C:ClientId"];

            });

        services.AddMvc();
        services.AddMemoryCache();

       
        services.AddControllers();

        // Start Registering and Initializing AutoMapper

        services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

        // End Registering and Initializing AutoMappe

    }

如何最好地验证访问令牌的范围?

感谢任何帮助。

这篇来自 Auth0 的文章有一个非常好的教程,介绍了如何创建自定义授权属性,该属性从令牌中获取范围声明(“scp”)并验证每个控制器方法的范围。如果每个服务只有 1 个范围,那么这当然可以在全球范围内完成。 https://auth0.com/docs/quickstart/backend/aspnet-core-webapi/01-authorization