发布项目后,Hangfire不再工作
问题描述:
我正在使用ASP.NET MVC 5. .NET Framework的版本为4.5。 当我发布我的项目后,Hangfire不再工作。所以我的循环任务不会工作。当我输入www。{myurl}/Hangfire时,我得到一个空白网站。 conenction字符串不会引发错误。发布项目后,Hangfire不再工作
config.UseSqlServerStorage("Server={myServer}.database.windows.net,1433;Database={myDatabase};User ID={myUserId};Password={MyPassword};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;");
config.UseServer();
那么问题出在哪里呢?当我在localhost上运行我的项目时,它工作正常。 我在本地和项目的已发布版本上使用相同的数据库。
答
默认拒绝对Hangfire仪表板的远程请求 - 在将其发布到生产环境之前,忘记授权是非常简单的。
您可以使用Hangfire.Dashboard.Authorization包来配置基于用户,角色,声明或基本身份验证的授权;或者创建您自己的授权过滤器,如下所示。
using Hangfire.Dashboard;
public class MyRestrictiveAuthorizationFilter : IAuthorizationFilter
{
public bool Authorize(IDictionary<string, object> owinEnvironment)
{
// In case you need an OWIN context, use the next line.
// `OwinContext` class is defined in the `Microsoft.Owin` package.
var context = new OwinContext(owinEnvironment);
return false; // or `true` to allow access
}
}
创建授权过滤器后,其注册:
app.UseHangfire(config =>
{
config.UseAuthorizationFilters(new MyRestrictiveAuthorizationFilter());
});
答
这不是很好的做法,但你可以使用下面的代码,允许所有用户
app.UseHangfire(config =>
{
config.UseAuthorizationFilters(); //allow all users to access the dashboard
});
代码from