Hangfire Dashboard .net核心 - 身份验证在本地运行,而不是在服务器上运行

时间:2018-05-29 22:24:53

标签: hangfire

我已根据建议实施了对Hangfire Dashboard的身份验证(无需身份验证即可在服务器上查看仪表板。)

它无法在服务器上运行,所以我只是将Authorize设置为始终为真:

在startup.cs中:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    //Other stuff here
    app.UseHangfireDashboard("/hangfire", new DashboardOptions
    {
        Authorization = new[] { new MyAuthorizationFilter() }
    });
}

在我自己的班上:

/// <summary>
/// Used for Hangfire Dashboard only
/// </summary>
public class MyAuthorizationFilter : IDashboardAuthorizationFilter
{
    public bool Authorize(DashboardContext context)
    {
        return true;
    }
}

然而,即使这会返回403错误 - 但它在本地工作正常。以前的auth也可以在本地运行,但不能在服务器上运行。

我搜索了Google,但找不到有此错误的人 - 我该如何解决?

感谢。

1 个答案:

答案 0 :(得分:0)

适用于以下情况:

class MyAuthorizationFilter : IDashboardAuthorizationFilter
    {
        public bool Authorize(DashboardContext context)
        {
            var httpContext = context.GetHttpContext();

            // Allow all authenticated users to see the Dashboard (potentially dangerous).
            return httpContext.User.Identity.IsAuthenticated;
        }
    }

在启动方法中,请如下使用:

   app.UseAuthentication();

        app.UseHangfireDashboard(options: new DashboardOptions()
        {
            Authorization = new IDashboardAuthorizationFilter[]
            {
                new MyAuthorizationFilter()
            }
        });

        app.UseHangfireServer();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });