即使用户已登录,httpcontext也会将用户身份返回为空,尝试为hangfire设置身份验证过滤器

问题描述 投票:0回答:2

我尝试通过仅允许具有管理员角色的用户访问/hangfire来配置hangfire身份验证,但这样做时,即使用户已登录,httpcontext中的用户也始终设置为null。

HangFireAuthorizationFilter:

    public class HangFireAuthorizationFilter : IDashboardAuthorizationFilter
    {

        public bool Authorize([NotNull] DashboardContext context)
        {
            var httpContextUser = context.GetHttpContext().User;
            var userIsAdmin = httpContextUser.IsInRole(SD.RoleAdmin);
            var userIsAuthenticated = httpContextUser.Identity.IsAuthenticated;
            return userIsAdmin && userIsAuthenticated;
        }
    }
}

启动.cs:

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDbInitializer dbInitializer)
        {
            app.UseHangfireServer();
            app.UseHangfireDashboard(
                pathMatch: "/hangfire",
                options: new DashboardOptions()
                {
                Authorization = new IDashboardAuthorizationFilter[] {new HangFireAuthorizationFilter(),

                }
            });
            // etc...
        }

它似乎在我的控制器和视图中工作正常:

@if (User.IsInRole(SD.RoleAdmin))
{
<div style="margin-top:0.5rem" align="center">
    <p>Admin user!</p>
</div>
}

工作正常并返回“管理员用户!”,但它没有返回过滤器中的用户,我不明白为什么......

c# asp.net asp.net-core httpcontext hangfire
2个回答
1
投票

通过移动这些方法解决了这个问题

            app.UseHangfireServer();
            app.UseHangfireDashboard(
                pathMatch: "/hangfire",
                options: new DashboardOptions()
                {
                Authorization = new IDashboardAuthorizationFilter[] {new HangFireAuthorizationFilter(),

                }
            });

在其他身份验证方法之后,如文档中所述...httpcontext 返回给用户相关信息,没有出现任何问题。


0
投票

我面临这个问题 dasboard context returns null user 我在所有身份验证方法之后移动了方法,但仍然存在同样的问题

您能否建议或发布启动顺序

© www.soinside.com 2019 - 2024. All rights reserved.