Skip to content

Instantly share code, notes, and snippets.

@meriturva
Last active December 26, 2020 15:50
Show Gist options
  • Save meriturva/18c6c1ca866b11ac06d5f19567f44e84 to your computer and use it in GitHub Desktop.
Save meriturva/18c6c1ca866b11ac06d5f19567f44e84 to your computer and use it in GitHub Desktop.
public class HangfireDashboardJwtAuthorizationFilter : IDashboardAuthorizationFilter
{
private static readonly string HangFireCookieName = "HangFireCookie";
private string role;
public HangfireDashboardJwtAuthorizationFilter(string role = null)
{
this.role = role;
}
public bool Authorize(DashboardContext context)
{
var httpContext = context.GetHttpContext();
var jwtFactory = httpContext.RequestServices.GetService(typeof(JwtFactory)) as JwtFactory;
var access_token = httpContext.Request.Cookies[HangFireCookieName];
if (String.IsNullOrEmpty(access_token))
{
return false;
}
try
{
var principal = jwtFactory.GetPrincipalFromToken(access_token);
if (!String.IsNullOrEmpty(this.role) && !principal.IsInRole(this.role)) {
return false;
}
}
catch (Exception e)
{
throw e;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment