Asp.NetCore6如何最简单地将JWTscp声明识别为角色?
创始人
2024-09-17 15:01:02
0

在Startup.cs的ConfigureServices方法中,使用AddAuthorization方法并在其中定义策略。在这里,我们可以将JWT的“scp”声明视为角色,同时将其转换为以“role:”开头的标准角色声明。我们可以定义一个自定义授权策略,通过读取“roles”声明并将其转换为标准角色声明来实现此目的。

代码示例:

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication()
            .AddJwtBearer(options =>
            {
                // JWT 配置
            });

    services.AddAuthorization(options =>
    {
        options.AddPolicy("PolicyName", policy =>
        {
            policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
            policy.RequireAuthenticatedUser();
            policy.RequireClaim("scp");

            policy.ForwardDefaultSelector = context =>
            {
                if (context.Request.Headers.ContainsKey("Authorization"))
                {
                    string header = context.Request.Headers["Authorization"];

                    if (header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
                    {
                        return JwtBearerDefaults.AuthenticationScheme;
                    }
                    else if (header.StartsWith("Basic ", StringComparison.OrdinalIgnoreCase))
                    {
                        return BasicAuthenticationDefaults.AuthenticationScheme;
                    }
                }

                return null;
            };
            
            policy.Requirements.Add(new RoleRequirement());
        });
    });
    
    services.AddSingleton();
}

public class RoleRequirementHandler : AuthorizationHandler
{
    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, RoleRequirement requirement)
    {
        if (context.User.HasClaim(c => c.Type == "scp"))
        {
            var scopes = context.User.FindFirst(c => c.Type == "scp").Value.Split(' ');

            foreach (string scope in scopes)
            {
                if (scope.StartsWith("role:"))
                {
                    var role = scope.Substring(5);
                    context.User.AddIdentity(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Role, role) }));
                }
            }
        }

        if (context.User.IsInRole(requirement.Role))
        {
            context.Succeed(requirement);
        }

        return Task.CompletedTask;
    }
}

public class RoleRequirement : IAuthorizationRequirement
{
    public RoleRequirement() { }

    public string Role { get; set; }
}

相关内容

热门资讯

解密关于!wepoker有人用... 解密关于!wepoker有人用过吗,wepoker怎么开辅助,一贯存在有挂(今日头条)1、首先打开辅...
分享开挂内幕!wepoker新... 分享开挂内幕!wepoker新号好一点吗,德普之星辅助工具如何打开,一贯真的是有挂(有挂详细)1)辅...
终于知道!fishpoker透... 终于知道!fishpoker透视,德扑圈透视,竟然真的有挂(有挂功能)1、完成辅助器v3.3的残局,...
大神推荐!wepoker有没有... 大神推荐!wepoker有没有透视方法,wepoker一直输的号能继续打吗,一贯真的是有挂(有挂总结...
玩家必看科普!aapoker安... 玩家必看科普!aapoker安装包怎么使用,hhpoker软件可以玩吗,确实存在有挂(有挂攻略)1、...
我来向大家传授!德普之星透视,... 我来向大家传授!德普之星透视,wepoker钻石怎么看底牌,都是是真的有挂(有挂方法)进入游戏-大厅...
热点推荐!wepoker有没有... 热点推荐!wepoker有没有插件,hhpoker德州挂真的有吗,切实是有挂(果真有挂)运辅助工具,...
记者揭秘!拱趴大菠萝自动计算机... 记者揭秘!拱趴大菠萝自动计算机器人,哈糖大菠萝挂法,都是是真的有挂(有挂细节)所有人都在同一条线上,...
实测分享!安装不了wepoke... 实测分享!安装不了wepoker,hhpoker视频巡查真的假的,果然真的是有挂(新版有挂)1、每一...
分享个大家!uupoker透视... 分享个大家!uupoker透视,wepoker安装教程,一贯是有挂(有挂方式)1、首先打开辅助器下载...