Open klinki opened 4 years ago
Are you using app.UseHangfireDashboard()
in your Configure(IApplicationBuilder app)
method in Startup
?
Do you need to use both()? Disclaimer - I haven't setup the Dashboard yet myself so I'm not sure all of what's needed - but I have seen documentation noting to use app.UseHangfireDashboard()
in your Configure(IApplicationBuilder app)
method
I was getting same error in .NET Core 3.1 with routing, and have fixed the problem by removing all app.UseHangfireXXX(...)
methods in my Configure()
block as mentioned here and here. My working solution with authorization has the lines below:
public IServiceProvider ConfigureServices(IServiceCollection services, ...)
{
...
services.Configure<AuthorizationOptions>(options =>
{
options.AddPolicy("somePolicy", policy =>
{
policy.RequireAuthenticatedUser();
});
});
services.AddHangfire(....);
services.AddHangfireServer(....);
...
}
public void Configure(IApplicationBuilder app, IBackgroundJobClient backgroundJobs, ...)
{
...
backgroundJobs.Enqueue(() => Console.WriteLine("Hello world from Hangfire!"));
app.UseCookiePolicy();
app.UseSession();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
...
endpoints.MapDefaultControllerRoute();
endpoints.MapHangfireDashboard("", new DashboardOptions
{
Authorization = new[] { new HangfireAuthorizationFilter("somePolicy"), },
...
});
...
});
}
public class HangfireAuthorizationFilter : IDashboardAuthorizationFilter
{
private string PolicyName { get; }
public HangfireAuthorizationFilter(string policyName)
{
this.PolicyName = policyName;
}
public bool Authorize(DashboardContext context)
{
var httpContext = context.GetHttpContext();
var authService = httpContext.RequestServices.GetRequiredService<IAuthorizationService>();
var authorizationResult = authService.AuthorizeAsync(httpContext.User, this.PolicyName).ConfigureAwait(false).GetAwaiter().GetResult();
var isAuthorized = authorizationResult.Succeeded;
if (!isAuthorized)
{
httpContext.ChallengeAsync().ConfigureAwait(false).GetAwaiter().GetResult();
}
return true; //always return true
}
}
Any updates on this? I am getting the same error while using following configuration:
endpoints.MapHangfireDashboard(string.Empty, new DashboardOptions { Authorization = new List<IDashboardAuthorizationFilter>(), StatsPollingInterval = 60000 }).RequireAuthorization();
Just to highlight - when using pattern = string.Empty
Hello,
I have problem with Hangfire dashboard returning error 500 for invalid routes.
It throws following exception:
I believe it should return HTTP 404 not found instead and not throw any exception.
I'm using ASP.NET Core and I'm configuring dashboard with
app.UseEndpoints(endpoints => endpoints.MapHangfireDashboard())