Skip to content
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

Restore user credentials in HttpBackgroundJob #14329

Merged
merged 10 commits into from
Mar 26, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public static Task ExecuteAfterEndOfRequestAsync(string jobName, Func<ShellScope
{
return Task.CompletedTask;
}
// Record the current logged in user.
var userPrincipal = httpContextAccessor.HttpContext.User.Clone();

// Fire and forget in an isolated child scope.
_ = ShellScope.UsingChildScopeAsync(async scope =>
Expand Down Expand Up @@ -59,7 +61,9 @@ public static Task ExecuteAfterEndOfRequestAsync(string jobName, Func<ShellScope

// Create a new 'HttpContext' to be used in the background.
httpContextAccessor.HttpContext = shellContext.CreateHttpContext();

// Restore the current user.
httpContextAccessor.HttpContext.User = userPrincipal;

// Here the 'IActionContextAccessor.ActionContext' need to be cleared, this 'AsyncLocal'
// field is not cleared by 'AspnetCore' and still references the previous 'HttpContext'.
var actionContextAccessor = scope.ServiceProvider.GetService<IActionContextAccessor>();
Expand Down
Loading