I am trying to integrate Hangfire into my ASP.NET Core Web API project for background processing. While basic setup works with memory storage, I need to configure it with SQL Server storage and restrict access to the dashboard.
Setting Up Hangfire Services
Here is my current setup in Program.cs:
// Add Hangfire services to DI container
builder.Services.AddHangfire(config =>
config.UseSqlServerStorage(builder.Configuration.GetConnectionString("HangfireConnection")));
// Add the processing server
builder.Services.AddHangfireServer();Restricting Dashboard Access
By default, the Hangfire dashboard is only accessible locally. How can I implement a custom IDashboardAuthorizationFilter to ensure only authorized admin users can view the dashboard in production?
- What is the recommended approach to extract authenticated claims inside the authorization filter?
- Are there performance implications when using SQL Server for high-frequency recurring jobs?
Hangfire can store its jobs in SQL Server and run a dashboard inside an ASP.NET Core app. The dashboard is an administrative interface, so protect it with your app’s existing authentication rather than exposing it to every visitor.
Install the packages
Add
Hangfire.AspNetCoreandHangfire.SqlServerto the project. Keep their versions aligned, and make sure the SQL Server connection string points to a database the application can access.Register Hangfire and the SQL Server storage
In
Program.cs, register Hangfire’s storage and server services. The server service is what processes queued jobs.Replace the authentication placeholder with the scheme your app actually uses, such as ASP.NET Core Identity or cookie authentication. The role check also assumes authenticated users receive an
Adminrole claim. If your app uses a different authorization rule, adapt the filter to match it.Put the connection string under the
Hangfirename in configuration, and keep production credentials in a secret store or environment variables rather than committing them to source control. Hangfire can create or update its SQL Server schema when it connects, so the database account needs the required permissions.With this setup, the dashboard is available at
/hangfireand is restricted to authenticated administrators. Avoid an allow-all dashboard filter in production; the dashboard can expose job details and provide controls for managing background work.