Hangfire: IDashboardAuthorizationFilter with .Net Core

Created on 14 Jul 2016  路  21Comments  路  Source: HangfireIO/Hangfire

Hi,
I'm playing with Hangfire and .NET core and everything seems ok (well done).
I'd like to know how to protect the dashboard for authenticated users.

I'm using IdentityServer with OpenId for the authentication and I'm trying to implement the logic into the my IDashboardAuthorizationFilter.

Unfortunately I didn't find a way to get the HttpContext or the current User.
How can I check if the current user is logged or not into my IDashboardAuthorizationFilter?

Thanks

bug question

Most helpful comment

@knopa, the DashboardContextExtensions was mistakenly named OwinDashboardContextExtensions (please see your screenshot). It has the GetOwinEnvironment method:

ASP.NET/OWIN applications

using Microsoft.Owin; // From the Microsoft.Owin package

public bool Authorize(DashboardContext context)
{
    var owinContext = new OwinContext(context.GetOwinEnvironment());

    // ...
}

ASP.NET Core applications

public bool Authorize(DashboardContext context)
{
    var httpContext = context.GetHttpContext();
    // ...
}

Now it's time to update the documentation :smile:

All 21 comments

I'm using a similar configuration (IdSrv v2). I simply add an AuthorizationFilter[] like:

var authorizationFilters = new IAuthorizationFilter[] 
                                                   { 
                                                       new AuthorizationFilter
                                                       {
                                                           Roles = "MyAuthorizedRole1;MyAuthorizedRole2"
                                                       },
                                                   };
app.UseHangfireDashboard("/path",
                                                   new Hangfire.DashboardOptions()
                                                   {
                                                       AuthorizationFilters = authorizationFilters
                                                   }

and then add those roles to the users that need it in the identityservr admin page (bt I believe what's really matter are the "Roles" of your Principal)

Hi @marcoCasamento
the problem here is that AuthorizationFilter doesn't exist and the IAuthorizationFilter is deprecated. The new one IDashboardAuthorizationFilter doesn't expose the User into the context so I can't check it (otherwise I don't have roles but claims)

I've just added extension methods, please see the referenced commit. Meanwhile you can cast the context to AspNetCoreDashboardContext class to get the HttpContext property.

class MyAuthorizationFilter : IDashboardAuthorizationFilter
{
    public bool Authorize(DashboardContext context)
    {
        var httpContext = ((AspNetCoreDashboardContext) context).HttpContext;
        return false;
    }
}

It works like this:

``` c#
internal class HangfireAuthorizationFilter : IDashboardAuthorizationFilter
{
private readonly string[] _roles;

public HangfireAuthorizationFilter(params string[] roles)
{
    _roles = roles;
}

public bool Authorize(DashboardContext context)
{
    var httpContext = ((AspNetCoreDashboardContext)context).HttpContext;
    var result = _roles.Aggregate(false, (current, role) => current || httpContext.User.IsInRole(role));

    return result;
}

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{

app.UseHangfireDashboard(options:new DashboardOptions
{
Authorization = new[]
{
new HangfireAuthorizationFilter("admin")
}
});
}
```

Or you can use the GetHttpContext extension method as well:

public bool Authorize(DashboardContext context)
{
    var httpContext = context.GetHttpContext();
    // ...

@odinserj where is this extension method?

Ah, it's in the Hangfire.Dashboard namespace, ReSharper pampered me

@odinserj which package? I dont see this extension in Dashboard namespace and my Resharper doesnt suggest me.

Hangfire.AspNetCore 1.6.0, in the AspNetCoreDashboardContextExtensions class.

@odinserj 袝谐芯 薪械褌 褌邪屑 )) 胁芯褌 褋泻褉懈薪褕芯褌

hangfire

Hm, it was included only to the Hangfire.Core project, and not to Hangfire.Core.NetStandard. So it exists in net45, but doesn't exist on netstandard1.3. @kroniak, thanks for persistence!

Fixed in b579546773d8ae57affc8a435954555ab1f23289.

How should be in Hangfire.Core v1.6.2?
How can I get httpContext?

@knopa 袛邪, 芯薪 褌邪屑 械褋褌褜

1a8487e92fd6ec1726137e6b00cea509

啸芯褌褜 褋褌褉械谢褜薪懈 薪芯 薪械褌 褝泻褋褌械薪褕懈薪邪 写谢褟 DashboardContext

@kroniak Any plan for Hangfire.Core?

@knopa this is to @odinserj

@knopa, the DashboardContextExtensions was mistakenly named OwinDashboardContextExtensions (please see your screenshot). It has the GetOwinEnvironment method:

ASP.NET/OWIN applications

using Microsoft.Owin; // From the Microsoft.Owin package

public bool Authorize(DashboardContext context)
{
    var owinContext = new OwinContext(context.GetOwinEnvironment());

    // ...
}

ASP.NET Core applications

public bool Authorize(DashboardContext context)
{
    var httpContext = context.GetHttpContext();
    // ...
}

Now it's time to update the documentation :smile:

@odinserj
Thanks, it works with owinContext.Request.User.IsInRole

The authenticated user is also avilable via the Authentication property :
owinContext.Authentication.User.IsInRole("...")

Was this page helpful?
0 / 5 - 0 ratings