2016-08-11 1 views
10

ich heruntergeladen habe nu-get Paket Hangfire.Dashboard.AuthorizationHangfire Dashboard-Authorization Config Problem

Ich versuche die OWIN basierte Autorisierung gemäß der Dokumentation wie folgt konfigurieren, aber ich erhalte Intellisense Fehler DashboardOptions.AuthorizationFilters is obsolete please use Authorization property instead

ich auch erhalten Intellisense Fehler 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(); 
     } 
    } 
} 

* UPDATE *

Da die oben nuget Paket funktioniert nicht ich versucht habe meine eigenen benutzerdefinierten Filter zu erstellen:

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

Wie erreiche ich nur Admin-Rollen beschränken das heißt, was die Syntax?

+0

Welche Version von HF Sie verwenden? Bitte zeigen Sie auch die Namespaces an, die Sie in die Klasse importiert haben. – Yogi

+0

@Yogi Hangfire-Kern ist 1.6.1 und die Hangfire.Dashborad.Authorization ist 2.1.0. Ich habe den Beitrag aktualisiert, um Namespaces anzuzeigen. – adam78

Antwort

1

auf diese Weise die Dashboard-Optionen definieren für mich gearbeitet -

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

ich die folgenden Namespaces importiert haben -

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

Ja, es zeigt mir die deprecated Warnung für AuthorizationFilters und vorschlagen zu verwenden Authorization, im Wesentlichen die IAuthorizationFilter Schnittstelle wird in Version 2.0 entfernt, und IDashboardAuthorizationFilter Schnittstelle muss verwendet werden.

Hierzu können Sie Ihren eigenen benutzerdefinierten Filter erstellen, der IDashboardAuthorizationFilter implementiert und stattdessen verwenden.

+0

Ich bekomme immer noch den Fehler 'Der Typ oder Namespace AuthorizationFilter existiert nicht im Namespace Hangfire.Dashboard'. Ich habe die gleichen Namensräume wie du benutzt? Sieht aus wie dieses Paket veraltet ist. – adam78

+0

Ich habe meinen eigenen Filter implementiert, aber wie beschränke ich mich nur auf Admin-Rollen - siehe meine Bearbeitung oben? – adam78

13

Bevor Sie Ihr Hangfire-Dashboard konfigurieren, müssen Sie sicherstellen, dass die Configure (app) -Methode in Ihrer Startup.cs-Klasse aufgerufen wird.

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

Dann in Ihre Auth-Config-Klasse kann man etwas so einfaches wie dies tun:

public class CustomAuthorizationFilter : IDashboardAuthorizationFilter 
{ 

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

     return false; 
    } 
} 
+0

Große Antwort! Vielen Dank! –

+0

Schön, dass dies für mich funktionierte Vote! –