-
-
Notifications
You must be signed in to change notification settings - Fork 746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
why IsAuthenticated and IsInRole always false in HangfireAuthorizationFilter? #269
Comments
Hi @urmatgit , In attach you can find a solution. Hope this help. HangfireAuthorizationFilter.cs public bool Authorize(DashboardContext context)
{
//TODO implement authorization logic
var httpContext = context.GetHttpContext();
string jwtToken = "";
var read = httpContext.Request.Query.TryGetValue("token", out var jwtTokenFromQuery);
if (read)
{
jwtToken = jwtTokenFromQuery.ToString();
CookieOptions options = new CookieOptions
{
Expires = DateTime.Now.AddMinutes(60)
};
httpContext.Response.Cookies.Append("token", jwtToken, options);
}
else
{
read = httpContext.Request.Cookies.TryGetValue("token", out jwtToken);
}
if (!read) return false;
var handler = new JwtSecurityTokenHandler();
var token = handler.ReadJwtToken(jwtToken);
if (token is null) return false;
var hangfireViewPermission =
token.Claims.Any(w => w.Value.Equals(Permissions.Hangfire.View));
return hangfireViewPermission; NavMenu.razor @if (_canViewHangfire)
{
<MudNavLink Href="@_jobsLink" Target="_blank" Icon="@Icons.Material.Outlined.Work">
@_localizer["Hangfire"]
</MudNavLink>
}
...
private string _accessToken;
private string _jobsLink;
protected override async Task OnParametersSetAsync()
{
...
_accessToken = await _localStorage.GetItemAsync<string>(StorageConstants.Local.AuthToken);
_jobsLink = $"/jobs?token={_accessToken}";
} |
ok, thanks! |
Refer to my PR #270 for a complete example with expiration time taken from JWT |
log in as an administrator
Startup.cs
...
services.AddHangfire(x => x.UseSQLiteStorage(_configuration.GetConnectionString("DefaultConnectionSqlite")));
services.AddHangfireServer();
...
...
app.UseHangfireDashboard("/jobs", new DashboardOptions
{
DashboardTitle = localizer["BlazorHero Jobs"],
Authorization = new[] { new HangfireAuthorizationFilter() }
});
...
public class HangfireAuthorizationFilter : IDashboardAuthorizationFilter
{
public bool Authorize(DashboardContext context)
{
//TODO implement authorization logic
...

The text was updated successfully, but these errors were encountered: