【问题标题】:How can I protect MVC Hangfire Dashboard如何保护 MVC Hangfire 仪表板
【发布时间】:2015-03-17 06:03:16
【问题描述】:

我正在使用 Visual Studio 2013 MVC,并安装了“Hangfire”来执行计划任务。 (http://hangfire.io/)

如何使用密码保护 Web 监控 UI 页面 (http://localhost/Hangfire)?

谢谢

【问题讨论】:

    标签: c# visual-studio model-view-controller hangfire


    【解决方案1】:

    请看documentation

    简而言之。 您可以使用已创建的授权过滤器或实施您自己的

    using Hangfire.Dashboard;
    
    public class MyRestrictiveAuthorizationFilter : IAuthorizationFilter
    {
        public bool Authorize(IDictionary<string, object> owinEnvironment)
        {
             // In case you need an OWIN context, use the next line.
             var context = new OwinContext(owinEnvironment);
             return false;
        }
    }
    

    附加信息:

    您也可以查看special package Hangfire.Dashboard.Authorization,其中包含您需要的逻辑

    【讨论】:

    • 完美 :) 这就是我需要的。有用。非常感谢
    【解决方案2】:

    让我给出 RestrictiveAuthorizationFilter 的完整代码: 这样您就可以随心所欲地处理授权。

    假设您添加了 OWINStartup 类。

    OWINStartup.cs

    using Owin;
    using Hangfire;
    using Hangfire.Dashboard;
    
    public class OWINStartup
    {
        public void Configuration(IAppBuilder app)
        {        
            GlobalConfiguration.Configuration.UseSqlServerStorage("String");
            DashboardOptions options = new DashboardOptions()
            {
                AuthorizationFilters = new IAuthorizationFilter[]
                {
                    new MyRestrictiveAuthorizationFilter()
                }
            };
            app.UseHangfireDashboard("/hangfire", options);
        }
    }
    

    RestrictiveAuthorizationFilter.cs

    using Hangfire.Dashboard;
    using System.Collections.Generic;
    using Microsoft.Owin;
    
    public class MyRestrictiveAuthorizationFilter : IAuthorizationFilter
    {
        public bool Authorize(IDictionary<string, object> owinEnvironment)
        {
            var context = new OwinContext(owinEnvironment);
    
            return context.Authentication.User.Identity.IsAuthenticated;
        }
    }
    

    注意:使用 System.Collections.通用;

    参考资料: https://github.com/HangfireIO/Hangfire/issues/202

    https://media.readthedocs.org/pdf/hangfire/latest/hangfire.pdf(第 20 页)

    Hangfire.Dashboard.Authorization 版本:2.1.0

    【讨论】:

      【解决方案3】:

      在你的 Startup.Cs 中设置它

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
          {
              //TODO
              app.UseHangfireDashboard("/hangfire", new DashboardOptions
              {
                  Authorization = new[] { new MyAuthorizationFilter() }
              });
              app.UseHangfireDashboard();
              var options = new BackgroundJobServerOptions { WorkerCount = 1 };
              app.UseHangfireServer(options);    }
      

      创建这个类,它允许经过身份验证的用户看到仪表板

      public class MyAuthorizationFilter : IDashboardAuthorizationFilter
      {
          public bool Authorize(DashboardContext context)
          {
              var httpContext = context.GetHttpContext();
      
              // Allow all authenticated users to see the Dashboard (potentially dangerous).
              return httpContext.User.Identity.IsAuthenticated;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-19
        • 2020-08-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多