【发布时间】:2017-05-19 09:08:59
【问题描述】:
我正在使用 HangFire 来安排作业,但是当我部署到 PROD 时,website/hangfire url 不起作用。我收到系统找不到指定的文件错误。
在本地主机上,我可以打开 URL。
我关注了这个网址:http://docs.hangfire.io/en/latest/quick-start.html
任何人都知道我错过了什么。
谢谢
【问题讨论】:
标签: c# scheduled-tasks hangfire
我正在使用 HangFire 来安排作业,但是当我部署到 PROD 时,website/hangfire url 不起作用。我收到系统找不到指定的文件错误。
在本地主机上,我可以打开 URL。
我关注了这个网址:http://docs.hangfire.io/en/latest/quick-start.html
任何人都知道我错过了什么。
谢谢
【问题讨论】:
标签: c# scheduled-tasks hangfire
Hangfire Dashboard 公开有关您的后台作业的敏感信息,包括方法名称和序列化参数,并让您有机会通过执行不同的操作(重试、删除、触发等)来管理它们。因此,限制访问非常重要到仪表板。
为了使其安全,默认情况下只允许本地请求,但是您可以通过传递您自己的 IAuthorizationFilter 接口实现来更改此设置,该接口的 Authorize 方法用于允许或禁止请求。第一步是提供您自己的实现。
http://docs.hangfire.io/en/latest/configuration/using-dashboard.html#configuring-authorization
【讨论】:
由于 Hangfire 仪表板会公开有关您的工作的敏感信息,其中包括方法名称和序列化参数。用户还可以执行不同的操作,如重试、触发、删除等。因此,对 Dashboard 的访问进行身份验证非常重要。
默认情况下,Hangfire 仅允许本地请求访问仪表板页面。为了为生产或测试用户或 UAT 用户提供适当的权限,请使用 hangfire 仪表板的 IDashboardAuthorizationFilter 接口添加您自己的授权实现。
http://docs.hangfire.io/en/latest/configuration/configuring-authorization.html
在下面查看我的示例代码
public class HangfireAuthorizationFilter : IDashboardAuthorizationFilter
{
private readonly string[] _roles;
public HangfireAuthorizationFilter(params string[] roles)
{
_roles = roles;
}
public bool Authorize(DashboardContext context)
{
var httpContext = ((AspNetCoreDashboardContext)context).HttpContext;
//Your authorization logic goes here.
return true; //I'am returning true for simplicity
}
}
Configure(IApplicationBuilder app, IHostingEnvironment env)方法中的Asp.net核心启动类变化
Configure(IApplicationBuilder app, IHostingEnvironment env){
......
app.UseHangfireServer();
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
DashboardTitle = "Sample Jobs",
Authorization = new[]
{
new HangfireAuthorizationFilter("admin")
}
});
......
}
【讨论】: