blazor-starter-kit icon indicating copy to clipboard operation
blazor-starter-kit copied to clipboard

Example of Hangfire authorization using token from navlink

Open nbiada opened this issue 4 years ago • 2 comments

nbiada avatar Aug 11 '21 07:08 nbiada

Should it validate token before granting access? @nbiada

gozilla-paradise avatar Sep 22 '21 09:09 gozilla-paradise

I have done it like this This method added to dashboard controller

 [Authorize(Policy = Permissions.Hangfire.View)]
        [HttpGet("jobdashboardurl")]
        [Produces(typeof(string))]
        public IActionResult GetJobDashBoardUrl()
        {
            HttpContext.Session.SetString(ApplicationConstants.Hangfire.SessionUserIdKey, Get<ICurrentUserService>().UserId);
            return Ok(ApplicationConstants.Hangfire.DashboardRoute);
        } 

Content of the authfilter

public class HangfireAuthorizationFilter : IDashboardAuthorizationFilter
    {

        public bool Authorize(DashboardContext context)
        {
            var httpContext = context.GetHttpContext();
            var service = httpContext.RequestServices.GetService<IAccountService>();
            
            var userId = httpContext.Session.GetString(ApplicationConstants.Hangfire.SessionUserIdKey);
            
            return !string.IsNullOrEmpty(userId) && service != null && service.AuthorizeAsync(userId, Permissions.Hangfire.View).Result;
        }
    }

and on client side onclick instead of href


private async void OpenJobDashboard()
    {
        var uri =await _api.Dashboard_GetJobDashBoardUrlAsync();
        _navigationManager.NavigateTo(uri, true);
    }

fgilde avatar Oct 13 '21 13:10 fgilde