gpt4 book ai didi

c# - asp.net 模拟如何与 System.Threading.Task 一起工作?

转载 作者:行者123 更新时间:2023-12-02 15:26:00 25 4
gpt4 key购买 nike

我想使用客户端用户的凭据访问数据库。

我的代码:

Task.Run(() => 
{
// Connect to DB.
}

我在 system.web 节点中有带有模拟属性的身份标签:

<identity impersonate="true" />

我在 system.webServer 节点中有验证标记:

<validation validateIntegratedModeConfiguration="false" />

我将身份验证设置为 Windows:

<authentication mode="Windows" />

我在 iis 中的应用程序上设置了模拟和窗口:

iis settings

我已将应用设置为使用直通:

enter image description here

我已重新启动 iis。

我不知道它是如何工作的,或者我完全误解了它。

最佳答案

根据您的评论,您似乎正在任务中运行System.Security.Principal.WindowsIdentity.GetCurrent().Name,并获取应用程序池的身份而不是模拟用户的身份。这是一个常见问题(请查看 this question I asked a while back )。

但是,有一个解决方法。如果您仍然希望任务以模拟用户的身份运行,则必须在任务运行之前保存当前用户的 token ,并尝试“重新模拟”任务的上下文,如下所示:

IntPtr currentUser = WindowsIdentity.GetCurrent().Token; // This token points to the impersonated user

Task.Run(() =>
{
using (WindowsIdentity.Impersonate(token))
{
// Connect to DB.
}
}

这可确保 Using block 中的任何内容都在模拟用户的身份下运行。

关于c# - asp.net 模拟如何与 System.Threading.Task 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32658593/

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