My .NET Core 8 web application uses Hangfire to schedule a few processes and authorized users have access to the Hangfire dashboard, particularly to the Recurring Jobs screen, when they want to trigger any of the jobs manually. The jobs are declared in this manner:
RecurringJob.AddOrUpdate<IBatchDataFileProcessor>(
title,
(processor) => processor.Run(),
cronString);
When a user triggers one of these jobs through the Recurring Jobs dashboard, I would like the triggered process to be able to identity the user who triggered it (and, when run on schedule, to be able to tell that it wasn’t triggered by a user). How can that be done?
I define an authorization filter that implements IDashboardAuthorizationFilter
. There, in the Authorize
method, via the context parameter, I’m able to access the user as a ClaimsPrincipal
,
var User = context.GetHttpContext()?.User;
I tried injecting an IHttpContextAccessor
into the process and getting the user from there, but its HttpContext
is null (which didn’t surprise me, since the process isn’t being called directly through a web request). So, when a dashboard user browses to the dashboard, I’d like to know either how to grab the User in the Hangfire authorization filter and save it somewhere from where, when that user triggers a process, it can be passed to the process; or else some other way for the process to get a hold of it. Is it possible?