1

I have created a new application using ASP.NET Core 6.0 Web API.

Implemented HangFire with basic authentication, everything works as expected locally, but after the deployment, Hangfire always returns an UnAuthorized error.

Here is my Program.cs file:

service.AddControllers();
service.AddEndpointsApiExplorer();
service.AddSwaggerExtension();

service.AddHangfire(configuration => configuration
            .UseSimpleAssemblyNameTypeSerializer()
            .UseRecommendedSerializerSettings()
            .UseMemoryStorage());

service.AddHangfireServer();

service.AddHostedService<HangFireHostedService>();
service.AddSingleton<IChannelManager, ChannelManager>();
service.AddSingleton<IDataSourceManager, DataSourceManager>();
service.AddSingleton<IDataService, DataService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.UseHangfireDashboard("/hangfire", new DashboardOptions()
{
    DashboardTitle = "Hangfire Dashboard",
    IgnoreAntiforgeryToken = true,
    Authorization = new[]{
    new HangfireCustomBasicAuthenticationFilter{
      User = app.Configuration["Hangfire:Username"],
                Pass = app.Configuration["Hangfire:Password"]
    }
}
});

app.MapControllers();
app.MapHangfireDashboard();
app.Run();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tabish Matin
  • 85
  • 1
  • 10
  • Please check the link https://stackoverflow.com/questions/41623551/asp-net-core-mvc-hangfire-custom-authentication . I think your problem will be solved. – Srijon Chakraborty Dec 18 '22 at 10:39

1 Answers1

1

use this example:

Step 1:

Install this package:

Hangfire.Dashboard.BasicAuthorization

Step 2:

Add this code in startup:

app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
    Authorization = new[] { new BasicAuthAuthorizationFilter(
                    new BasicAuthAuthorizationFilterOptions
                    {
                        RequireSsl = false,
                        SslRedirect = false,
                        LoginCaseSensitive = true,
                        Users = new[]
                        {
                            new BasicAuthAuthorizationUser
                            {
                                Login = "Admin",
                                PasswordClear = "123"

                            }
                        }
                    }) }
});
ALNAJJAR
  • 292
  • 3
  • 11