2015-04-04 27 views
8

我正在迟发型但每当我尝试导航到http://MyApp/hangfire,它重定向我到我的应用程序的登录页面,虽然我没有登录。为什么迟发型需要验证我的MVC的Web应用程序中查看仪表盘

我没有明确配置任何授权要求...例如我在web.config中找到了下面的内容,但是为了实现这个目的,我尝试了一下。

<location path="hangfire"> 
<system.web> 
    <authorization> 
    <allow roles="Administrator" /> 
    <deny users="*" /> 
    </authorization> 
</system.web> 

从理论上讲,这是我想要的东西,当我登录到我的主要的Web应用程序,我将与Administrator角色身份登录所以这个规则应该工作。

但我是否有配置在web.config与否,每当我试图浏览到http://MyApp/hangfire,它重定向我到我的应用程序登录页面,在web.config配置:

<authentication mode="Forms"> 
    <forms loginUrl="~/Account/Login" timeout="960" /> 
</authentication> 

它不会在我的本地机器上执行此操作,只是当我发布到我的主机时。当我登录时,HangFire是否无法识别我的主应用程序提供的身份验证Cookie?我认为一般来说,hangfire应用程序不需要身份验证,那么还有什么其他配置可以认为它的确如此?

更新1:

我加入了授权过滤器每hangfire docs,但同样的事情发生。这是我在Startup.cs代码:

using Hangfire; 
using Hangfire.Logging; 
using Hangfire.Dashboard; 
using Hangfire.SqlServer; 
using Microsoft.Owin; 
using OTIS.Web.AppCode; 
using OTISScheduler.AppServ; 
using Owin; 
using System.Web.Security; 

[assembly: OwinStartup(typeof(OTIS.Web.App_Start.Startup))] 
namespace OTIS.Web.App_Start 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) { 

      app.UseHangfire(config => { 
       config.UseSqlServerStorage("DefaultConnection"); 
       config.UseServer(); 

       //Dashboard authorization 
       config.UseAuthorizationFilters(new AuthorizationFilter 
       { 
        Users = "USERA", // allow only specified users (comma delimited list) 
        Roles = "Account Administrator, Administrator" // allow only specified roles(comma delimited list) 
       }); 


      }); 

      LogProvider.SetCurrentLogProvider(new StubLogProviderForHangfire()); 

      GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0 }); 

      var scheduleTasksInitializer = new ScheduleTasksInitializer(); 

      scheduleTasksInitializer.ScheduleTasks(); 
     } 
    } 
} 

更新2:

每更detailed instructions showing basic authentication,我也试过这个......仍然没有luck..redirects我到我的应用程序的登录页面。

config.UseAuthorizationFilters(
new BasicAuthAuthorizationFilter(
    new BasicAuthAuthorizationFilterOptions 
    { 
     // Require secure connection for dashboard 
     RequireSsl = false, 
     SslRedirect = false, 

     // Case sensitive login checking 
     LoginCaseSensitive = true, 

     // Users 
     Users = new[] 
     { 
      new BasicAuthAuthorizationUser 
      { 
       Login = "MyLogin", 

       // Password as plain text 
       PasswordClear = "MyPwd" 
      } 
     } 
    }));   

回答

1

按照设计我相信。
请参阅docs for the dashboard

默认情况下,Hangfire允许仅对本地请求访问仪表板页面。

奇怪的是我与这个有一天,一件事情打交道要注意的是,如果你使用的是Autofac依赖注入,那么你需要确保你配置正确的顺序项目。具体迟发型后其他认证,而且,在我的情况,MembershipReboot其他OAuth的东西了。
花了相当多的尝试和错误。

+0

感谢输入,但我把这些代码中还是一样的结果。我没有使用Autofac或OAuth ...只是基本的asp.net基本成员资格。任何其他想法? – 2015-04-04 22:40:42

+0

对于仪表板的所有文档目前不工作,希望只是临时问题。请您更新链接? – cpoDesign 2016-01-09 20:55:26

8

终于搞定了。我创建了自己的AuthorizationFilter类(见下文)。 然后我将它传递给Startup中的MapHangfireDashboard方法。CS配置方法(见下面的那个)

public class HangFireAuthorizationFilter : IAuthorizationFilter 
{ 
    public bool Authorize(IDictionary<string, object> owinEnvironment) 
    { 
     bool boolAuthorizeCurrentUserToAccessHangFireDashboard = false; 

     if (HttpContext.Current.User.Identity.IsAuthenticated) 
     { 
      if(HttpContext.Current.User.IsInRole("Account Administrator")) 
       boolAuthorizeCurrentUserToAccessHangFireDashboard = true; 
     } 

     return boolAuthorizeCurrentUserToAccessHangFireDashboard; 
    } 
} 

要映射迟发型到自定义网址,并指定AuthorizationFilter使用方法:

public void Configuration(IAppBuilder app) { 

    //Get from web.config to determine to fire up hangfire scheduler or not 

    app.UseHangfire(config => { 
     config.UseSqlServerStorage("DefaultConnection"); 
     config.UseServer();    
    }); 

    //map hangfire to a url and specify the authorization filter to use to allow access 
    app.MapHangfireDashboard("/Admin/jobs", new[] { new HangFireAuthorizationFilter() }); 

} 
+0

查看@Baris Gomleksizoglu针对较新版本的答案 – 2016-08-30 09:41:45

17

随着你应该使用IDashboardAuthorizationFilter新版本。随着using语句,它看起来就像这样:

using System.Web; 
using Hangfire.Annotations; 
using Hangfire.Dashboard; 

namespace Scheduler.Hangfire 
{ 
    public class HangFireAuthorizationFilter : IDashboardAuthorizationFilter 
    { 
     public bool Authorize([NotNull] DashboardContext context) 
     { 
      //can add some more logic here... 
      return HttpContext.Current.User.Identity.IsAuthenticated; 
     } 
    } 
} 

然后在配置部分:

app.UseHangfireDashboard("/jobs", new DashboardOptions() 
     { 
      Authorization = new [] {new HangFireAuthorizationFilter()} 
     }); 
+0

您是否知道在用户未通过身份验证时重定向到页面的方法? IE浏览器。到登录页面? – MartinM 2016-11-08 12:56:43

+0

您可以使用标准的.net表单认证来实现它:在web.config中添加“authentication”标签,并在“FormsAuthentication_OnAuthenticate”中检查global.asax中的登录用户。 – Baris 2016-12-13 08:26:00

相关问题