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



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

在之前的一篇文章中,关于配置web应用程序以使其能够访问多个受保护的api的最佳方法是什么;将两个服务合并到一个应用程序注册中,并公开不同的作用域">

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

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的这篇文章有一个非常好的教程,介绍如何创建一个自定义Authorize属性,该属性从令牌中获取作用域声明("scp"(,并验证每个控制器方法的作用域。如果每个服务只有一个作用域,那么这肯定可以在全局范围内完成。https://auth0.com/docs/quickstart/backend/aspnet-core-webapi/01-authorization

相关内容

最新更新