问题 Hangfire Dashboard授权配置不起作用


我已经下载了nu-get包 Hangfire.Dashboard.Authorization

我正在尝试按照以下文档配置基于OWIN的授权,但我得到intellisense错误 DashboardOptions.AuthorizationFilters is obsolete please use Authorization property instead

我也得到intellisense错误 The type or namespace AuthorizationFilter and ClaimsBasedAuthorizationFilterd not be found

using Hangfire.Dashboard;
using Hangfire.SqlServer;
using Owin;
using System;

namespace MyApp
{
    public class Hangfire
    {
       public static void ConfigureHangfire(IAppBuilder app)
        {
           GlobalConfiguration.Configuration
           .UseSqlServerStorage(
               "ApplicationDbContext",
                new SqlServerStorageOptions 
                  { QueuePollInterval = TimeSpan.FromSeconds(1) });

           var options = new DashboardOptions
           {
               AuthorizationFilters = new[]
               {
                  new AuthorizationFilter { Users = "admin, superuser", Roles = "advanced" },
                  new ClaimsBasedAuthorizationFilter("name", "value")
               }
           };

           app.UseHangfireDashboard("/hangfire", options);
           app.UseHangfireServer();
        }
    }
}

*更新*

由于上面的nuget包没有用,我试图创建自己的自定义过滤器:

public class HangfireAuthorizationFilter : IAuthorizationFilter
{
    public bool Authorize(IDictionary<string, object> owinEnvironment)
    {
        // In case you need an OWIN context, use the next line,
        // `OwinContext` class is the part of the `Microsoft.Owin` package.
        var context = new OwinContext(owinEnvironment);

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

如何仅限制管理员角色,即语法是什么?


13042
2017-08-11 08:06


起源

您使用的是哪个版本的HF?另请显示您在课程中导入的命名空间。 - Yogi
@Yogi Hangfire核心是1.6.1,Hangfire.Dashborad.Authorization是2.1.0。我已经更新了帖子以显示命名空间。 - adam78


答案:


在配置hangfire仪表板之前,您需要确保在Startup.cs类中调用Configure(app)方法。

  public partial class Startup
{
    private static readonly ILog log = 
        LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod
    ().DeclaringType);


    public void Configuration(IAppBuilder app)
    {

        //Hangfire Config
        GlobalConfiguration.Configuration.UseSqlServerStorage
            ("HangFireJobs");
        app.UseHangfireServer();

        log.Debug("Application Started");

        ConfigureAuth(app);


        //this call placement is important
        var options = new DashboardOptions
        {
            Authorization = new[] { new CustomAuthorizationFilter() }
        };
        app.UseHangfireDashboard("/hangfire", options);
    }
}

然后在你的auth配置类中,你可以做一些简单的事情:

  public class CustomAuthorizationFilter : IDashboardAuthorizationFilter
{ 

    public bool Authorize(DashboardContext context)
    {
        if (HttpContext.Current.User.IsInRole("Admin"))
        {
            return true; 
        }

        return false; 
    }
}

14
2017-10-01 06:32



很棒的答案!谢谢! - Razvan Ghena
很好,这对我起了投票! - Frank Odoom


以这种方式定义仪表板选项对我有用 -

    var options = new DashboardOptions
    {
        AuthorizationFilters = new List<IAuthorizationFilter>
       {
          new Hangfire.Dashboard.AuthorizationFilter { Users = "admin, superuser", Roles = "advanced" },
          new Hangfire.Dashboard.ClaimsBasedAuthorizationFilter("name", "value")
       }
    };

我导入了以下命名空间 -

using System;
using Owin;
using Hangfire;
using Hangfire.Dashboard;
using System.Collections.Generic;
using Hangfire.SqlServer;

是的它正在向我展示 deprecated 警告 AuthorizationFilters 并建议使用 Authorization,基本上是 IAuthorizationFilter 接口将在2.0版本中删除,并且 IDashboardAuthorizationFilter 必须使用接口。

为此,您可以创建自己的自定义过滤器实现 IDashboardAuthorizationFilter 并改为使用它。

public class MyAuthorizationFilter : IDashboardAuthorizationFilter
{
    public bool Authorize(DashboardContext context)
    {
        //Implement
    }
}

1
2017-08-11 09:51



我仍然得到错误 The type or namespace AuthorizationFilter does not exist in the namespace Hangfire.Dashboard。我和你自己使用了相同的命名空间?看起来这个包已经过时了。 - adam78
我已经实现了自己的过滤器,但是如何限制只使用管理员角色 - 请参阅上面的编辑? - adam78