ASP.NETCore的AuthorizeAttribute是如何工作的?
创始人
2024-09-17 20:00:14
0

ASP.NET Core的AuthorizeAttribute提供了一种在控制器和其操作方法上进行授权的方法。当一个请求来到服务器时,AuthorizeAttribute会检查该请求是否包含正确的授权令牌。它可以通过验证用户的身份(通过用户名和密码等凭据)或令牌(通过OAuth)来确定用户是否被授权访问特定的资源。

具体实现需要以下步骤:

  1. 在Startup.cs中设置身份验证方案和策略:
services.AddAuthentication("MyScheme")
    .AddScheme("MyScheme", null);
services.AddAuthorization(options =>
{
    options.AddPolicy("MyPolicy", policy =>
        policy.Requirements.Add(new MyRequirement()));
});
  1. 实现Handler和Requirement:
public class MySchemeHandler : AuthenticationHandler
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public MySchemeHandler(IHttpContextAccessor httpContextAccessor,
        IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder,
        ISystemClock clock) : base(options, logger, encoder, clock)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    protected override async Task HandleAuthenticateAsync()
    {
        var identity = new ClaimsIdentity(new[]
        {
            new Claim(ClaimTypes.Name, "test")
        }, MyScheme.SchemeName);

        var principal = new ClaimsPrincipal(identity);
        var ticket = new AuthenticationTicket(principal, MyScheme.SchemeName);
        return AuthenticateResult.Success(ticket);
    }
}

public class MySchemeOptions : AuthenticationSchemeOptions
{
}

public class MyRequirement : IAuthorizationRequirement
{
    public bool RequiresAuthentication => true;

    public bool RequiresAuthorization => true;
}

public class MyAuthorizationHandler : AuthorizationHandler
{
    protected override async Task HandleRequirementAsync(
        AuthorizationHandlerContext context, MyRequirement requirement)
    {
        var claimsIdentity = context.User.Identity as ClaimsIdentity;
        if (claimsIdentity == null)
        {
            context.Fail();
            return;
        }

        var nameClaim = claimsIdentity.FindFirst(ClaimTypes.Name);
        if (nameClaim == null)
        {
            context.Fail();
            return;
        }

        if (nameClaim.Value == "test")
        {
            context.Succeed(requirement);
            return;
        }

        context.Fail();
    }
}
  1. 使用AuthorizeAttribute验证:
[Authorize(Policy = "MyPolicy")]
public IActionResult MyAction()
{
    return Ok();
}

在执行MyAction之前,AuthorizeAttribute会检查身份验证是否成功,如果成功,则调用MyAuthorizationHandler来验证授权。

通过以上实现,我们可以对不同的用户和资源进行授权访问的管理。

相关内容

热门资讯

透视演示!hhpoker辅助软... 透视演示!hhpoker辅助软件是真的么(WePoKer俱乐部)总是有辅助神器(哔哩哔哩)1、hhp...
透视操作!德州私人局怎么透视(... 透视操作!德州私人局怎么透视(AApoker插件)好像是有辅助软件(哔哩哔哩)1.德州私人局怎么透视...
透视练习!wpk俱乐部怎么作必... 透视练习!wpk俱乐部怎么作必弊(WePoKer钻石)好像真的是有辅助攻略(哔哩哔哩)wpk俱乐部怎...
透视技法!菠萝辅助器免费版的特... 透视技法!菠萝辅助器免费版的特点(HHpoker透视挂)一贯有辅助方法(哔哩哔哩)1)菠萝辅助器免费...
透视课程!hhpoker辅助挂... 透视课程!hhpoker辅助挂(WPK模拟器)果然存在有辅助教程(哔哩哔哩)hhpoker辅助挂破解...
透视手筋!hh poker软件... 透视手筋!hh poker软件(AApoker设置)切实是真的有辅助插件(哔哩哔哩)hh poker...
透视法门!wepoker辅助软... 透视法门!wepoker辅助软件价格(WPK有透视)切实是有辅助方法(哔哩哔哩)1、不需要AI权限,...
透视机巧!有没有人wepoke... 透视机巧!有没有人wepoker(AApoker辅助器)好像有辅助方法(哔哩哔哩)1、进入游戏-大厅...
透视技法!wepoker有用吗... 透视技法!wepoker有用吗(AApoker控制牌)都是是真的有辅助神器(哔哩哔哩)1、超多福利:...
透视要领!智星菠萝辅助(HHp... 透视要领!智星菠萝辅助(HHpoker教程)竟然有辅助技巧(哔哩哔哩)该软件可以轻松地帮助玩家将智星...