.Net Core Web API项目中的Hangfire仪表板

时间:2018-09-20 14:39:54

标签: asp.net-core hangfire

我们有一个.Net Core 2.0 Web API项目。我在那加了篝火。我们的项目中没有任何网页,我使用JWT进行授权。因此,我无法使用Authorize(DashboardContext上下文)进行hangfire的授权。有什么办法可以在URL上传递某种API密钥来授权用户使用仪表板?

谢谢

1 个答案:

答案 0 :(得分:-1)

实现IAuthorizationDashboardFilter类

public class MyAuthorizationFilter : IDashboardAuthorizationFilter
{
     public bool Authorize(DashboardContext context)
     {
       var httpContext = context.GetHttpContext();

       // here you can access the current principle 
       return httpContext.User.Identity.IsAuthenticated;
   }
}

在使用任何身份验证方法之后,将过滤器像这样在OWIN管道中注册。然后,已登录的用户声明将在过滤器中可用

public void Configuration(IAppBuilder app)
{
    app.UseCookieAuthentication(...); // Authentication - first
    app.UseHangfireDashboard("/hangfire", new DashboardOptions
    {
         Authorization = new [] { new MyAuthorizationFilter() }
    });       // Hangfire - last
}