gpt4 book ai didi

c# - 将 QueueBackgroundWorkItem 与用户身份一起使用?

转载 作者:太空狗 更新时间:2023-10-29 19:46:49 27 4
gpt4 key购买 nike

我正在使用 HostingEnvironment.QueueBackgroundWorkItem 在 ASP.Net 应用程序的后台运行工作,基于 Scott Hanselman 的博客文章 How to run Background Tasks in ASP.NET .

我想以当前用户身份运行后台任务。我已尝试传递 WindowsPrincipal 并在操作中设置 Thread.CurrentPrincipal,但这并没有导致操作以当前用户身份执行。

这是否可能,或者使用 HostingEnvironment 是否总是意味着以应用程序池身份运行?

编辑

与我原来的问题不完全一致,但我也尝试通过 CallContext.LogicalSetData() 和 CallContext.LogicalGetData() 传递一个值。在 Get 端,该值始终为 null。

编辑 #2

还在排队端试过这个:

using (HostingEnvironment.Impersonate(windowsIdentity.Token))
{
HostingEnvironment.QueueBackgroundWorkItem(work);
}

当真正完成工作时,Action中当前的WindowsIdentity仍然是app pool identity。

最佳答案

您“必须”使用“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();

您可以了解更多操作方法 here

关于c# - 将 QueueBackgroundWorkItem 与用户身份一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31125971/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com