Hangfire icon indicating copy to clipboard operation
Hangfire copied to clipboard

DashboardContext delivers a incorrect User.Identity status

Open AliGuemues opened this issue 4 years ago • 3 comments

Hello,

in our ASP .NET Application we use a new simplified way for authentication in our StartupExtensions.cs:

` services .AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApp(objConfiguration.GetSection("Authentication:AzureAd"));

            services
                .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApi(objConfiguration.GetSection("Authentication:AzureAdJWTApi"));`

Right after these changes the Access to the Hangfire site are not working anymore. We configure Hangfire like this:

if (optionsApplicationSettings.EnableHangfire) objApp.UseHangfireDashboard("/hangfire", new DashboardOptions() { Authorization = new[] { new HangFireDashBoardAuthFilter() }, StatsPollingInterval = 30000 });

In the past everything works well with Hangfire. I already installed the newest version via Nuget.

public class HangFireDashBoardAuthFilter : IDashboardAuthorizationFilter { public bool Authorize([NotNull] DashboardContext context) { HttpContext httpcontext = context.GetHttpContext(); return httpcontext.User.Identity.IsAuthenticated; } }

Now in the HangFireDashBoardAuthFilter class the state of the User object is not correct anymore. It always returns false for "IsAuthenticated". Which is not correct. Something is wrong with the HttpContext.

What I am doing wrong? Any ideas?

Thanks and greetings.

AliGuemues avatar Oct 08 '21 08:10 AliGuemues

If it works everywhere else except the dashboard, I’d start with checking the order in which your middlewares are added to the pipeline. If Hangfire dashboard is added before the auth middleware, the former will never see authentication state.

pieceofsummer avatar Oct 08 '21 15:10 pieceofsummer

Facing the same issue. User.Identity.IsAuthenticated is always false.

This is my program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));

builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
builder.Services.RegisterInfrastructure();
builder.Services.RegisterApplication();
builder.Services.RegistorHttpClientFactory(builder.Configuration);
builder.Services.AddControllers();
builder.Services.AddHangfire(x => x.UsePostgreSqlStorage(builder.Configuration.GetConnectionString("Hangfire")));
builder.Services.AddHangfireServer();
builder.Services.AddMemoryCache();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseWebAssemblyDebugging();
}
else
{
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseHangfireDashboard("/hangfire", new DashboardOptions{Authorization = new []{new HangfireAuthorizationFilter()},
    IgnoreAntiforgeryToken = true});

app.MapRazorPages();
app.MapControllers();

app.MapFallbackToFile("index.html");
app.Services
    .CreateScope()
    .ServiceProvider
    .GetRequiredService<PSContext>()
    .Database
    .EnsureCreated();

RecurringJob.AddOrUpdate<ReminderService>("finalize", service => service.SendRemainder(), Cron.Daily,
    new RecurringJobOptions
    {
        TimeZone = TimeZoneInfo.Local
    });

app.Run();

Here is my custom filter.

public class HangfireAuthorizationFilter : IDashboardAuthorizationFilter
{
    public bool Authorize([NotNull] DashboardContext context)
    {
        return context.GetHttpContext().User.Identity!.IsAuthenticated;

    }
}

Any ideas on why this is working?

Thanks

mohammedsouleymane avatar May 16 '23 16:05 mohammedsouleymane

顺序问题,请按照如下顺序添加即可:

app.UseAuthentication();
app.UseAuthorization();
app.UseHangfireDashboard();

EminemJK avatar Aug 01 '25 07:08 EminemJK