首页 > 解决方案 > Hangfire 仪表板授权重定向以登录

问题描述

我已将 Hangfire 添加到 dotnetcore 项目中。我添加了仪表板授权过滤器。

public class HangfireDashboardAuthorizationFilter : IDashboardAuthorizationFilter
{
    public bool Authorize(DashboardContext context)
    {
        var httpContext = context.GetHttpContext();
        if (!httpContext.User.Identity.IsAuthenticated)
        {
            return false;
        }

        var userRole = httpContext.User.FindFirst(ClaimTypes.Role)?.Value;
        return userRole == Roles.Administrator.ToDisplayName();
    }
}

这工作正常。但是当用户未经授权时,我只会收到 401。它不会重定向到我的登录页面。如何使此过滤器导致重定向?

标签: hangfire

解决方案


推荐阅读