I've downloaded the nu-get package Hangfire.Dashboard.Authorization
I'm trying configure the OWIN based authorization as per the docs as follows but I get intellisense error DashboardOptions.AuthorizationFilters is obsolete please use Authorization property instead
I also get intellisense error
The type or namespace AuthorizationFilter and ClaimsBasedAuthorizationFilterd not be found
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();
}
}
}
* UPDATE *
Since the above nuget package doesnt work I've attempted to create my own custom filter:
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;
}
}
How do I restrict to only Admin roles i.e what is the syntax?
You need to make sure the Configure(app) method is called in your Startup.cs class before configuring your hangfire dashboard.
Then in your auth config class you can do something as simple as this :
Defining the dashboard options in this way worked for me -
I have imported the following namespaces -
Yes it is showing me the
deprecated
warning forAuthorizationFilters
and suggest to useAuthorization
, basically theIAuthorizationFilter
interface is going to removed in version 2.0, andIDashboardAuthorizationFilter
interface has to be used.For this you can create your own custom filter implementing
IDashboardAuthorizationFilter
and use this instead.