为什么 hangfire 仪表板在开发中工作而不在部署中工作

why hangfire dashboard work in development and not work in deploy

这是访问 hangfire 仪表板生产时的响应

{"error":"The antiforgery system has the configuration value AntiforgeryOptions.Cookie.SecurePolicy = Always, but the current request is not an SSL request."}

这是使用的配置

services.AddHangfire(config => config
        .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
        .UseSimpleAssemblyNameTypeSerializer()
        .UseRecommendedSerializerSettings()
        .UseDefaultTypeSerializer()
        .UseSqlServerStorage(configuration.GetConnectionString("BackOffice")));;

        var sqlStorage = new SqlServerStorage(configuration.GetConnectionString("BackOffice"));
        JobStorage.Current = sqlStorage;
        services.AddHangfireServer();
        services.AddHttpClient();

您需要将 app.UseHangfireDashboard("/hangfire") 设置添加到您的 startup.cs 文件。

注意这里的顺序很重要!

// This method gets called by the runtime. Use this 
method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, 
IBackgroundJobClient backgroundJobs, 
IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        }


    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", 
   "TheHockeyLabMn.WebApi v1"));

    app.UseHttpsRedirection();
        
    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();
    app.UseElmah(); 
        app.UseHangfireDashboard("/hangfire", new 
     DashboardOptions
    {
        Authorization = new[] { new MyAuthorizationFilter() }
    });
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapHangfireDashboard();

    });
}

请记住您的过滤器,我只是在此处设置为真作为测试。

public class MyAuthorizationFilter : 
IDashboardAuthorizationFilter
 {
    public MyAuthorizationFilter()
    {

    }
    public bool Authorize(DashboardContext context)
    {
        var httpContext = context.GetHttpContext();

        // Allow all authenticated users to see the Dashboard (potentially dangerous).
        return true;//httpContext.User.Identity.IsAuthenticated;
    }
}