gpt4 book ai didi

c# - Hangfire 仪表板授权配置不工作

转载 作者:可可西里 更新时间:2023-11-01 08:21:16 26 4
gpt4 key购买 nike

我已经下载了 nu-get 包 Hangfire.Dashboard.Authorization

我正在尝试按照如下文档配置基于 OWIN 的授权,但我收到智能感知错误 DashboardOptions.AuthorizationFilters is obsolete please use Authorization property instead

我也收到智能感知错误未找到类型或命名空间 AuthorizationFilter 和 ClaimsBasedAuthorizationFilterd

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

我如何限制只有管理员角色,即语法是什么?

最佳答案

在配置 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);
}
}

然后在您的身份验证配置类中,您可以做一些像这样简单的事情:

  public class CustomAuthorizationFilter : IDashboardAuthorizationFilter
{

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

return false;
}
}

关于c# - Hangfire 仪表板授权配置不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38890688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com