Using Dashboard¶

Hangfire Dashboard is a place where you could find all the information about your background jobs. It is written as an OWIN middleware (if you are not familiar with OWIN, don’t worry), so you can plug it into your ASP.NET, ASP.NET MVC, Nancy, ServiceStack application as well as use OWIN Self-Host feature to host Dashboard inside console applications or in Windows Services.

Adding Dashboard¶

Additional package required for ASP.NET + IIS

Before moving to the next steps, ensure you have Microsoft.Owin.Host.SystemWeb package installed, otherwise you’ll have different strange problems with the Dashboard.

OWIN Startup class is intended to keep web application bootstrap logic in a single place. In Visual Studio 2013 you can add it by right clicking on the project and choosing the Add / OWIN Startup Class menu item.
Using Dashboard - 图1
If you have Visual Studio 2012 or earlier, just create a regular class in the root folder of your application, name it Startup and place the following contents:

  1. using Hangfire;
  2. using Microsoft.Owin;
  3. using Owin;
  4.  
  5. [assembly: OwinStartup(typeof(MyWebApplication.Startup))]
  6.  
  7. namespace MyWebApplication
  8. {
  9. public class Startup
  10. {
  11. public void Configuration(IAppBuilder app)
  12. {
  13. // Map Dashboard to the `http://<your-app>/hangfire` URL.
  14. app.UseHangfireDashboard();
  15. }
  16. }
  17. }

After performing these steps, open your browser and hit the http://<your-app>/hangfire URL to see the Dashboard.

Authorization configuration required

By default Hangfire allows access to Dashboard pages only for local requests. In order to give appropriate rights for production use, please see the Configuring Authorization section.

Configuring Authorization¶

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 IDashboardAuthorizationFilter interface, whose Authorize method is used to allow or prohibit a request. The first step is to provide your own implementation.

Don’t want to reinvent the wheel?

User, role and claims -based as well as basic access authentication-based (simple login-password auth) authorization filters available as a NuGet packageHangfire.Dashboard.Authorization.

  1. public class MyAuthorizationFilter : IDashboardAuthorizationFilter
  2. {
  3. public bool Authorize(DashboardContext context)
  4. {
  5. // In case you need an OWIN context, use the next line, `OwinContext` class
  6. // is the part of the `Microsoft.Owin` package.
  7. var owinContext = new OwinContext(context.GetOwinEnvironment());
  8.  
  9. // Allow all authenticated users to see the Dashboard (potentially dangerous).
  10. return owinContext.Authentication.User.Identity.IsAuthenticated;
  11. }
  12. }

For ASP.NET Core environments, use the GetHttpContext extension method defined in the Hangfire.AspNetCore package.

  1. public class MyAuthorizationFilter : IDashboardAuthorizationFilter
  2. {
  3. public bool Authorize(DashboardContext context)
  4. {
  5. var httpContext = context.GetHttpContext();
  6.  
  7. // Allow all authenticated users to see the Dashboard (potentially dangerous).
  8. return httpContext.User.Identity.IsAuthenticated;
  9. }
  10. }

The second step is to pass it to the UseHangfireDashboard method. You can pass multiple filters, and the access will be granted only if all of them return true.

  1. app.UseHangfireDashboard("/hangfire", new DashboardOptions
  2. {
  3. Authorization = new [] { new MyAuthorizationFilter() }
  4. });

Method call order is important

Place a call to the UseHangfireDashboard method after other authentication methods in your OWIN Startup class. Otherwise authentication may not work for you.

  1. public void Configuration(IAppBuilder app)
  2. {
  3. app.UseCookieAuthentication(...); // Authentication - first
  4. app.UseHangfireDashboard(); // Hangfire - last
  5. }

Read-only view¶

The read-only dashboard view prevents users from changing anything, such as deleting or enqueueing jobs. It is off by default, meaning that users have full control. To enable it, set the IsReadOnlyFunc property of the DashboardOptions:

  1. app.UseHangfireDashboard("/hangfire", new DashboardOptions
  2. {
  3. IsReadOnlyFunc = (DashboardContext context) => true
  4. });

Change URL Mapping¶

By default, UseHangfireDashboard method maps the Dashboard to the /hangfire path. If you want to change this for one reason or another, just pass your URL path.

  1. // Map the Dashboard to the root URL
  2. app.UseHangfireDashboard("");
  3.  
  4. // Map to the `/jobs` URL
  5. app.UseHangfireDashboard("/jobs");

By default, Back to site link (top-right corner of Dashboard) leads you to the root URL of your application. In order to change it, use the DashboardOptions class.

  1. // Change `Back to site` link URL
  2. var options = new DashboardOptions { AppPath = "http://your-app.net" };
  3. // Make `Back to site` link working for subfolder applications
  4. var options = new DashboardOptions { AppPath = VirtualPathUtility.ToAbsolute("~") };
  5.  
  6. app.UseHangfireDashboard("/hangfire", options);

Multiple Dashboards¶

You can also map multiple dashboards that show information about different storages.

  1. var storage1 = new SqlServerStorage("Connection1");
  2. var storage2 = new SqlServerStorage("Connection2");
  3.  
  4. app.UseHangfireDashboard("/hangfire1", new DashboardOptions(), storage1);
  5. app.UseHangfireDashboard("/hangfire2", new DashboardOptions(), storage2);

原文:

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