I'm using Hangfire to schedule some jobs, but when deployed to production the Dashboard does not work.
I've already implemented the authorization process (https://docs.hangfire.io/en/latest/configuration/using-dashboard.html#configuring-authorization), but it didn't help. Here's my relevant code:
HangfireDashboardAuthorizationFilter.cs
public class HangfireDashboardAuthorizationFilter : IDashboardAuthorizationFilter
{
public bool Authorize(DashboardContext context)
{
var hasClaim = context.GetHttpContext().User.Claims
.Any(claim => claim.Type == "customClaimType" &&
claim.Value == "customClaimValue");
return hasClaim;
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services){
// Add Hangfire services.
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseMemoryStorage()
);
// Add the processing server as IHostedService
services.AddHangfireServer();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IAntiforgery antiforgery){
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(routes =>
{
routes.MapAreaControllerRoute(
name: "areas",
areaName: "areas",
pattern: "{area:exists}/{controller=Default}/{action=Index}/{id?}");
routes.MapDefaultControllerRoute();
routes.MapRazorPages();
});
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
Authorization = new []{ new HangfireDashboardAuthorizationFilter() },
StatsPollingInterval = 30000,
});
}
While debugging, everything works as expected. When the application is deployed to production, the filter works for the purpose of denying requests if an unauthenticated user (or an user that does not have the necessary claims) tries to access the "/hangfire" endpoint, and an expected 401 response is returned. However, when the user has the necessary claims, a 200 response code is returned with no content.
I've already been googling for a solution but did not find anything, any help will be appreciated. Thanks.