我已下载了nu-get软件包Hangfire.Dashboard.Authorization
我正在尝试按照以下文档配置基于OWIN的授权,但我得到智能感知错误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包无效,我试图创建自己的自定义过滤器:
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;
}
}
如何仅限制管理员角色,即语法是什么?
答案 0 :(得分:16)
在配置hangfire控制面板之前,您需要确保在Startup.cs类中调用Configure(app)方法。
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配置类中,你可以做一些简单的事情:
public class CustomAuthorizationFilter : IDashboardAuthorizationFilter
{
public bool Authorize(DashboardContext context)
{
if (HttpContext.Current.User.IsInRole("Admin"))
{
return true;
}
return false;
}
}
答案 1 :(得分:1)
以这种方式定义仪表板选项对我有用 -
var options = new DashboardOptions
{
AuthorizationFilters = new List<IAuthorizationFilter>
{
new Hangfire.Dashboard.AuthorizationFilter { Users = "admin, superuser", Roles = "advanced" },
new Hangfire.Dashboard.ClaimsBasedAuthorizationFilter("name", "value")
}
};
我导入了以下命名空间 -
using System;
using Owin;
using Hangfire;
using Hangfire.Dashboard;
using System.Collections.Generic;
using Hangfire.SqlServer;
是的,它向我显示了deprecated
的{{1}}警告并建议使用AuthorizationFilters
,基本上{2.0}接口将在2.0版中删除,{{{}必须使用1}}接口。
为此,您可以创建自己的自定义过滤器,实现Authorization
并使用它。
IAuthorizationFilter