覆盖ASP.Net Core中的AuthorizeAttribute并响应Json状态

Red*_*ane 5 asp.net-core asp.net-core-webapi

我正在从ASP.Net Framework转向ASP.Net Core.

在带有Web API 2项目的ASP.Net Framework中,我可以像这样自定义AuthorizeAttribute:

public class ApiAuthorizeAttribute : AuthorizationFilterAttribute
{
    #region Methods

    /// <summary>
    ///     Override authorization event to do custom authorization.
    /// </summary>
    /// <param name="httpActionContext"></param>
    public override void OnAuthorization(HttpActionContext httpActionContext)
    {
        // Retrieve email and password.
        var accountEmail =
            httpActionContext.Request.Headers.Where(
                    x =>
                        !string.IsNullOrEmpty(x.Key) &&
                        x.Key.Equals("Email"))
                .Select(x => x.Value.FirstOrDefault())
                .FirstOrDefault();

        // Retrieve account password.
        var accountPassword =
            httpActionContext.Request.Headers.Where(
                    x =>
                        !string.IsNullOrEmpty(x.Key) &&
                        x.Key.Equals("Password"))
                .Select(x => x.Value.FirstOrDefault()).FirstOrDefault();

        // Account view model construction.
        var filterAccountViewModel = new FilterAccountViewModel();
        filterAccountViewModel.Email = accountEmail;
        filterAccountViewModel.Password = accountPassword;
        filterAccountViewModel.EmailComparision = TextComparision.Equal;
        filterAccountViewModel.PasswordComparision = TextComparision.Equal;

        // Find the account.
        var account = RepositoryAccount.FindAccount(filterAccountViewModel);

        // Account is not found.
        if (account == null)
        {
            // Treat the account as unthorized.
            httpActionContext.Response = httpActionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);

            return;
        }

        // Role is not defined which means the request is allowed.
        if (_roles == null)
            return;

        // Role is not allowed 
        if (!_roles.Any(x => x == account.Role))
        {
            // Treat the account as unthorized.
            httpActionContext.Response = httpActionContext.Request.CreateResponse(HttpStatusCode.Forbidden);

            return;
        }

        // Store the requester information in action argument.
        httpActionContext.ActionArguments["Account"] = account;
    }

    #endregion

    #region Properties

    /// <summary>
    ///     Repository which provides function to access account database.
    /// </summary>
    public IRepositoryAccount RepositoryAccount { get; set; }

    /// <summary>
    ///     Which role can be allowed to access server.
    /// </summary>
    private readonly byte[] _roles;

    #endregion

    #region Constructor

    /// <summary>
    ///     Initialize instance with default settings.
    /// </summary>
    public ApiAuthorizeAttribute()
    {
    }

    /// <summary>
    ///     Initialize instance with allowed role.
    /// </summary>
    /// <param name="roles"></param>
    public ApiAuthorizeAttribute(byte[] roles)
    {
        _roles = roles;
    }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

在我自定义的AuthorizeAttribute中,我可以检查帐户是否有效,并将HttpStatusCode返回给客户端.

我正在尝试在ASP.Net Core中做同样的事情,但我没有OnAuthorization来覆盖.

我怎样才能在ASP.Net Framework中实现同样的功能?

谢谢,

Dav*_*ine 13

你接近这个错误了.从来没有真正鼓励为此编写自定义属性或扩展现有属性.使用ASP.NET核心角色仍然是系统的一部分,用于向后兼容,但现在也不鼓励它们.

关于一些驱动架构变化以及在此处应该使用的方式,有一个很棒的2部分系列.如果你仍然希望依赖角色,你可以这样做,但我建议使用政策.

要连接策略,请执行以下操作:

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthorization(options =>
    {
        options.AddPolicy(nameof(Policy.Account), 
                          policy => policy.Requirements.Add(new AccountRequirement()));
    });

    services.AddSingleton<IAuthorizationHandler, AccountHandler>();
}
Run Code Online (Sandbox Code Playgroud)

Policy为方便起见,我创建了一个枚举.

public enum Policy { Account };
Run Code Online (Sandbox Code Playgroud)

装饰入口点如下:

[
    HttpPost,
    Authorize(Policy = nameof(Policy.Account))
]
public async Task<IActionResult> PostSomething([FromRoute] blah)
{
}
Run Code Online (Sandbox Code Playgroud)

AccountRequirement只是一个占位符,它需要实现的IAuthorizationRequirement接口.

public class AccountRequirement: IAuthorizationRequirement { }
Run Code Online (Sandbox Code Playgroud)

现在我们只需要创建一个处理程序并将其连接到DI.

public class AccountHandler : AuthorizationHandler<AccountRequirement>
{
    protected override Task HandleRequirementAsync(
        AuthorizationHandlerContext context,
        AccountRequirement requirement)
    {
        // Your logic here... or anything else you need to do.
        if (context.User.IsInRole("fooBar"))
        {
            // Call 'Succeed' to mark current requirement as passed
            context.Succeed(requirement);
        }

        return Task.CompletedTask;
    }
}
Run Code Online (Sandbox Code Playgroud)

其他资源