2016-08-11 50 views
10

Tôi đã tải về nu-get gói Hangfire.Dashboard.AuthorizationHangfire Dashboard Authorization Config Không làm việc

Tôi đang cố gắng cấu hình OWIN dựa uỷ quyền theo các tài liệu như sau nhưng tôi nhận được lỗi IntelliSense DashboardOptions.AuthorizationFilters is obsolete please use Authorization property instead

Tôi cũng nhận được lỗi IntelliSense 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(); 
     } 
    } 
} 

* CẬP NHẬT *

Vì công phu NuGet gói doesnt trên, chúng tôi đã cố gắng để tạo bộ lọc tùy chỉnh của riêng tôi:

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

Làm thế nào để hạn chế để chỉ vai trò quản lý tức là cú pháp là gì?

+0

Phiên bản nào của HF bạn đang sử dụng? Ngoài ra hãy hiển thị các không gian tên bạn đã nhập trong lớp. – Yogi

+0

@Yogi Hangfire core là 1.6.1 và Hangfire.Dashborad.Authorization là 2.1.0. Tôi đã cập nhật bài đăng để hiển thị không gian tên. – adam78

Trả lời

1

Xác định các tùy chọn bảng điều khiển theo cách này làm việc cho tôi -

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

tôi đã nhập không gian tên sau -

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

Có nó được hiển thị cho tôi những cảnh báo deprecated cho AuthorizationFilters và đề nghị sử dụng Authorization, về cơ bản giao diện IAuthorizationFilter sẽ bị xóa trong phiên bản 2.0 và giao diện IDashboardAuthorizationFilter phải được sử dụng.

Đối với điều này, bạn có thể tạo bộ lọc tùy chỉnh của riêng mình triển khai IDashboardAuthorizationFilter và sử dụng tùy chọn này thay thế.

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

Tôi vẫn nhận được lỗi 'Loại hoặc không gian tên AuthorizationFilter không tồn tại trong không gian tên Hangfire.Dashboard'. Tôi đã sử dụng cùng một không gian tên như chính mình? Có vẻ như gói này đã lỗi thời. – adam78

+0

Tôi đã triển khai bộ lọc của riêng mình nhưng làm cách nào để hạn chế chỉ vai trò quản trị viên - xem chỉnh sửa của tôi ở trên? – adam78

13

Bạn cần đảm bảo phương thức Configure (app) được gọi trong lớp Startup.cs của bạn trước khi định cấu hình bảng điều khiển tạm dừng.

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

Sau đó, trong lớp cấu hình auth của bạn, bạn có thể làm điều gì đó đơn giản như này:

public class CustomAuthorizationFilter : IDashboardAuthorizationFilter 
{ 

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

     return false; 
    } 
} 
+0

Câu trả lời hay! Cảm ơn! –

+0

Điều này đã giúp tôi bình chọn! –

Các vấn đề liên quan