Api .net核心-如何在AuthorizationHandler上返回403?

Api .net核心-如何在AuthorizationHandler上返回403?,api,authentication,asp.net-core,.net-core,jwt,Api,Authentication,Asp.net Core,.net Core,Jwt,我实现了自定义授权处理程序。 在我检查i时,用户可以解析并处于活动状态 如果用户未处于活动状态,则我希望返回403状态 protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, ValidUserRequirement requirement) { var userId = context.User.FindFirstValue( ClaimTypes.NameIde

我实现了自定义授权处理程序。 在我检查i时,用户可以解析并处于活动状态

如果用户未处于活动状态,则我希望返回403状态

protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, ValidUserRequirement requirement)
{
    var userId = context.User.FindFirstValue( ClaimTypes.NameIdentifier );

    if (userId != null)
    {
        var user = await _userManager.GetUserAsync(userId);

        if (user != null)
        {
            _httpContextAccessor.HttpContext.AddCurrentUser(user);

            if (user.Active)
            {
                context.Succeed(requirement);
                return;
            }
            else
            {
                _log.LogWarning(string.Format("User ´{1}´ with id: ´{0} isn't active", userId, user.UserName), null);
            }
        }
        else
        {
            _log.LogWarning(string.Format("Can't find user with id: ´{0}´", userId), null);
        }
    } else
    {
        _log.LogWarning(string.Format("Can't get user id from token"), null);
    }

    context.Fail();

    var response = _httpContextAccessor.HttpContext.Response;
    response.StatusCode = 403;

}

但是我收到了401。你能帮帮我吗

您能在函数末尾检查一下吗?在某些情况下,我在自定义中间件中使用它将状态代码重写为401,但在您的场景中也应该可以

var filterContext = context.Resource as AuthorizationFilterContext;
var response = filterContext?.HttpContext.Response;
response?.OnStarting(async () =>
{
    filterContext.HttpContext.Response.StatusCode = 403;
//await response.Body.WriteAsync(message, 0, message.Length); only when you want to pass a message
});

根据单一责任原则,我们不应该使用
HandleRequirementAsync()
方法重定向响应,我们应该使用中间件或控制器来实现。如果将重定向逻辑放在
HandleRequirementAsync()
中,那么如果要在查看页面中使用它呢

您可以将重定向相关的代码删除到其他地方(外部),现在您可以插入
IAuthorizationService
来授权任何您喜欢的内容,甚至是基于资源的授权:

public class YourController : Controller{

    private readonly IAuthorizationService _authorizationService;
    public YourController(IAuthorizationService authorizationService)
    {
        this._authorizationService = authorizationService;
    }

    [Authorize("YYY")]
    public async Task<IActionResult> Index()
    {
        var resource  /* = ... */ ;
        var x = await this._authorizationService.AuthorizeAsync(User,resource , "UserNameActiveCheck");

        if (x.Succeeded)
        {
            return View();
        }
        else {
            return new StatusCodeResult(403);
        }
    }

}
公共类控制器:控制器{
私有只读IAuthorizationService\u授权服务;
公共控制器(IAAuthorizationService授权服务)
{
此._authorizationService=authorizationService;
}
[授权(“YYY”)]
公共异步任务索引()
{
变量资源/*=…*/;
var x=等待此消息。_authorizationService.authorizationAsync(用户,资源,“UserNameActiveCheck”);
如果(x.successed)
{
返回视图();
}
否则{
返回新的StatusCodeResult(403);
}
}
}

授权!=认证。401(未授权)是正确的状态代码,而不是403(禁止)。你说得对!我只尝试返回403,所以现在我可以通过身份验证扩展此方法;当客户端未进行身份验证时返回HTTP 401,并暗示在有效身份验证之后可能返回成功响应,而当客户端尽管提供了身份验证(例如,已验证帐户的权限不足),但不允许访问资源时返回HTTP 403。[维基百科]。403应该是“授权处理程序”中的状态代码。我希望对所有api函数都这样做。对于每种方法来说,似乎比集中进行一次要做更多的工作。