【发布时间】:2015-02-02 10:46:02
【问题描述】:
我有一个由 IIS 托管的 Web 应用程序。它配置了表单认证和匿名认证,并启用了模拟。 应用程序池帐户是网络服务。匿名帐户是 Costa。 Costa 可以访问数据库。 NetworkService 无法访问数据库。
问题是Request线程(父线程)可以访问数据库,而子线程不能访问。
解决这个问题。我将主线程的 Windows 标识对象发送到子线程,然后调用 Impersonate()。模拟的意思是“用模拟帐户分配当前线程 Windows 标识。 我的问题:这是一个好习惯吗?有风险吗?
\\Request thread code (Parent thread)
\\WindowsIdentity.GetCurrent() return Costa identity (impersonated)
requestFields.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
ThreadPool.QueueUserWorkItem(LogRequest, requestFields);
--
\\ Sub thread code that works
RequestFields requestFields = (RequestFields)obj;
HttpRequest httpRequest = requestFields.Request;
var impersonationContext = ((WindowsIdentity)requestFields.CurrentPrincipal.Identity).Impersonate();
.
.
.
impersonationContext.Undo();
【问题讨论】:
-
您没有说您使用的是哪个版本的 IIS。带有集成管道的 IIS7 或更高版本不再直接在配置中支持模拟,主要是因为异步请求的问题(即,当您使用 asyc/await 时,请求可以在不再具有模拟的不同线程上恢复)。所以我不确定你所说的“启用模拟”是什么意思。
标签: c# .net multithreading impersonation