将QueueBackgroundWorkItem与用户标识一起使用?

Phi*_*ler 8 c# asp.net impersonation

我正在使用HostingEnvironment.QueueBackgroundWorkItemASP.Net应用程序的后台运行工作,基于Scott Hanselman的博客文章如何在ASP.NET中运行后台任务.

我想将后台任务作为当前用户的身份运行.我已尝试在操作中传递WindowsPrincipal并设置Thread.CurrentPrincipal,但这不会导致Action作为当前用户执行.

这是可能的,还是使用HostingEnvironment总是暗示作为应用程序池标识运行?

编辑

不完全是我的原始问题,但我也试图通过CallContext.LogicalSetData()和CallContext.LogicalGetData()传递一个值.在Get侧,值始终为null.

编辑#2

也在排队方面尝试了这个:

using (HostingEnvironment.Impersonate(windowsIdentity.Token))
{
     HostingEnvironment.QueueBackgroundWorkItem(work);
}
Run Code Online (Sandbox Code Playgroud)

当工作实际完成时,Action中的当前WindowsIdentity仍然是应用程序池标识.

小智 1

您“必须”使用“HostingEnvironment”的任何具体原因?

或者您是否尝试过使用 WindowsImpersonationContext ?

System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext = 
    ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();

//Insert your code that runs under the security context of the authenticating user here.

impersonationContext.Undo();
Run Code Online (Sandbox Code Playgroud)

您可以在此处了解更多操作方法