HangFire dashboard not displaying in PROD
Asked Answered
S

3

9

I am using HangFire to schedule jobs but when I deployed to PROD, the website/hangfire url is not working. I am getting The system cannot find the file specified error.

On localhost, I am able to open the URL.

I followed this URL: http://docs.hangfire.io/en/latest/quick-start.html

Anyone know what I am missing.

Thanks

Southport answered 4/1, 2017 at 22:55 Comment(0)
S
13

Hangfire Dashboard exposes sensitive information about your background jobs, including method names and serialized arguments as well as gives you an opportunity to manage them by performing different actions – retry, delete, trigger, etc. So it is really important to restrict access to the Dashboard.

To make it secure by default, only local requests are allowed, however you can change this by passing your own implementations of the IAuthorizationFilter interface, whose Authorize method is used to allow or prohibit a request. The first step is to provide your own implementation.

http://docs.hangfire.io/en/latest/configuration/using-dashboard.html#configuring-authorization

Southport answered 4/1, 2017 at 23:27 Comment(0)
M
4

As Hangfire dashboard exposes sensitive information about your job which includes method names and serialized arguments. Also user can perform different actions like retry, trigger, delete etc. So it is very important to authenticate access to Dashboard.

By default Hangfire allows access to Dashboard pages only for local requests. In order to give appropriate rights for production or testing or UAT users, add your own implementation of authorization using the IDashboardAuthorizationFilter interface for the hangfire dashboard.

http://docs.hangfire.io/en/latest/configuration/configuring-authorization.html

See my sample code below

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 startup class changes in Configure(IApplicationBuilder app, IHostingEnvironment env) method

Configure(IApplicationBuilder app, IHostingEnvironment env){
      ......
        app.UseHangfireServer();
        app.UseHangfireDashboard("/hangfire", new DashboardOptions
        {               
            DashboardTitle = "Sample Jobs",
            Authorization = new[]
            {
                new  HangfireAuthorizationFilter("admin")
            }
        });
      ......
      }
Murmuration answered 25/2, 2020 at 17:14 Comment(0)
C
0

Maybe a late answer but might be usefull.

In my case, I had this code :

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

It wasn't working on prod. I realized that when I copy the application dll from the bin folder, it takes the debug configuration and doesn't start the hangfire. When I publish the application via visual studio and than copy the DLL from the bin folder of published folder, it works correctly.

Cytolysis answered 26/4, 2022 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.