Hangfire Dashboard in .Net Core Web API Project

Viewed 1782

We have a .Net Core 2.0 Web API project. I have added the hangfire there. We don't have any web page in the project and I use JWT for authorization. So I'm not able to do the authorization for hangfire using the Authorize(DashboardContext context). Is there any way we can pass some sort of API key on the url to authorize the user for dashboard?

Thanks

1 Answers

Implement an IAuthorizationDashboardFilter class. Try this post here. You need to pass JWT token with each request, try storing token in a cookie or as a query string parameter. Then you can pull that from request context and decide whether user is authorized

public class MyAuthorizationFilter : IDashboardAuthorizationFilter
{
     public bool Authorize(DashboardContext context)
     {
       var httpContext = context.GetHttpContext();
       var token = context.Request.Cookies["access-token"];
       // check token validity and set authentication accordingly. 
       return httpContext.User.Identity.IsAuthenticated;
   }
}

Register the filter in your OWIN pipeline like this, after whatever authentication method you are using. Then the logged in user claim will be available in the filter

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