【问题标题】:Hangfire Dashboard Authorization Config Not workingHangfire仪表板授权配置不起作用
【发布时间】:2016-08-11 08:06:25
【问题描述】:

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

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

我也收到智能感知错误 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;
    }
}

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

【问题讨论】:

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

标签: c# asp.net-mvc owin hangfire


【解决方案1】:

在配置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);
    }
}

然后在您的身份验证配置类中,您可以执行以下简单操作:

  public class CustomAuthorizationFilter : IDashboardAuthorizationFilter
{ 

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

        return false; 
    }
}

【讨论】:

  • 很好的答案!谢谢!
  • 1000% 感谢您的简单回答!这是简单的事情。
  • 谢谢。这种方法易于使用。请确保在 endpoints.MapHangfireDashboard(); 之前使用上述答案端点配置
  • @frank 对我来说就是这样,“订单很重要”!
【解决方案2】:

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

    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;

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

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

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

        //Netcore example
        return dashboardContext.GetHttpContext().User.Identity.IsAuthenticated;
    }
}

【讨论】:

  • 我仍然收到错误The type or namespace AuthorizationFilter does not exist in the namespace Hangfire.Dashboard。我使用了和你一样的命名空间?看起来这个包已经过时了。
  • 我已经实现了自己的过滤器,但如何限制为仅管理员角色 - 请参阅上面的编辑?
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-01
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多