Hangfire仪表板授权配置不起作用

Hangfire仪表板授权配置不起作用

本文介绍了Hangfire仪表板授权配置不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've downloaded the nu-get package Hangfire.Dashboard.Authorization

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

I'm trying configure the OWIN based authorization as per the docs as follows but I get intellisense error 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包不起作用,因此我尝试创建自己的自定义过滤器:

Since the above nuget package doesnt work I've attempted to create my own custom filter:

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

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

How do I restrict to only Admin roles i.e what is the syntax?

推荐答案

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

You need to make sure the Configure(app) method is called in your Startup.cs class before configuring your hangfire dashboard.

  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 config类中,您可以执行以下操作:

Then in your auth config class you can do something as simple as this :

  public class CustomAuthorizationFilter : IDashboardAuthorizationFilter
{

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

        return false;
    }
}

这篇关于Hangfire仪表板授权配置不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:22