HangFire 仪表板在生产环境中不显示

8
我正在使用HangFire来安排任务,但是当我部署到生产环境时,网站/hangfire的URL无法正常工作。我收到了系统找不到指定的文件错误提示。
在本地主机上,我可以打开该URL。
我按照以下URL操作:http://docs.hangfire.io/en/latest/quick-start.html 请问有人知道我错过了什么吗?
谢谢。
3个回答

11

4
默认情况下,Hangfire仅允许本地请求访问Dashboard页面,因为它向用户公开有关作业的敏感信息,包括方法名称和序列化参数。为了给生产或测试或UAT用户适当的权限,请使用IDashboardAuthorizationFilter接口添加自己实现的授权。您可以通过访问以下链接查看更多信息:http://docs.hangfire.io/en/latest/configuration/configuring-authorization.html。以下是我的示例代码。
public class HangfireAuthorizationFilter : IDashboardAuthorizationFilter
{
    private readonly string[] _roles;

    public HangfireAuthorizationFilter(params string[] roles)
    {
        _roles = roles;
    }

    public bool Authorize(DashboardContext context)
    {
        var httpContext = ((AspNetCoreDashboardContext)context).HttpContext;

        //Your authorization logic goes here.

        return true; //I'am returning true for simplicity
    }
}

Asp.net core的启动类在Configure(IApplicationBuilder app, IHostingEnvironment env)方法中发生了变化。

Configure(IApplicationBuilder app, IHostingEnvironment env){
      ......
        app.UseHangfireServer();
        app.UseHangfireDashboard("/hangfire", new DashboardOptions
        {               
            DashboardTitle = "Sample Jobs",
            Authorization = new[]
            {
                new  HangfireAuthorizationFilter("admin")
            }
        });
      ......
      }

0

也许有点晚了,但可能会有用。

在我的情况下,我有这段代码:

  public class Startup
  {
    public void Configuration(IAppBuilder app)
    {
      #if !DEBUG
          app.UseHangfireDashboard("/hangfire", new DashboardOptions
          {
            Authorization = new[] { new HangfireAuthFilter() }
          });
       #endif
    }
   }
 }

在生产环境中它没有工作。 我意识到当我从bin文件夹复制应用程序dll时,它采用调试配置并且无法启动hangfire。 当我通过Visual Studio发布应用程序,然后从已发布文件夹的bin文件夹中复制DLL时,它可以正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接