Skip to content

Commit

Permalink
Add AllowAnonymousDashboardAuthorizationFilter for Hangfire Dashboard…
Browse files Browse the repository at this point in the history
… access
  • Loading branch information
nenad0707 committed Jun 7, 2024
1 parent 03df6dc commit 362f162
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
19 changes: 19 additions & 0 deletions TodoApi/Filters/AllowAnonymousDashboardAuthorizationFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Hangfire.Dashboard;

namespace TodoApi.Filters;

/// <summary>
/// An authorization filter that allows all users to access the Hangfire dashboard.
/// </summary>
public class AllowAnonymousDashboardAuthorizationFilter : IDashboardAuthorizationFilter
{
/// <summary>
/// Authorizes access to the Hangfire dashboard.
/// </summary>
/// <param name="context">The dashboard context.</param>
/// <returns>Always returns true, allowing all users to access the dashboard.</returns>
public bool Authorize(DashboardContext context)
{
return true; // Allow all users to access the dashboard
}
}
7 changes: 6 additions & 1 deletion TodoApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AspNetCoreRateLimit;
using Hangfire;
using TodoApi.Filters;
using TodoApi.StartupConfig;

var builder = WebApplication.CreateBuilder(args);
Expand Down Expand Up @@ -46,7 +47,11 @@

app.MapHealthChecks("/health").AllowAnonymous();

app.UseHangfireDashboard("/hangfire"); // Add Hangfire Dashboard
// Configure Hangfire Dashboard to allow anonymous access
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
Authorization = new[] { new AllowAnonymousDashboardAuthorizationFilter() }
});

app.UseHangfireJobs(); // Use the new middleware to set up Hangfire jobs

Expand Down

0 comments on commit 362f162

Please sign in to comment.