【问题标题】:why hangfire dashboard work in development and not work in deploy为什么hangfire仪表板在开发中工作而不在部署中工作
【发布时间】:2021-12-31 22:25:07
【问题描述】:

这是访问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();

【问题讨论】:

标签: c# asp.net-core hangfire


【解决方案1】:

注意这里的顺序很重要!

// 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();

    });
}

你的过滤器应该是裸露的,我只是在这里设置 true 作为测试。

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;
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我添加了但还是不行
    猜你喜欢
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    • 2022-07-01
    • 2014-06-27
    • 1970-01-01
    • 2023-03-12
    • 2017-01-12
    • 1970-01-01
    相关资源
    最近更新 更多