I have some recurring jobs in my .NET Core application 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?