【发布时间】:2026-02-11 08:45:02
【问题描述】:
有人可以帮我理解“模仿”的概念吗?
我的理解是,当模拟发生时,代码代表某个身份执行。
因此,对于网页,只要禁用模拟,网页将始终在其配置的帐户下运行。
如果启用,我可以“覆盖”其默认帐户并设置我希望 Web 应用程序在哪个帐户下运行。
因此,如果我使用的是 IIS7 并且我有以下内容:
- 身份设置为自定义帐户“user1”的应用程序池。
- 一个 asp.net 网站,其应用程序池设置为上述之一,并且已禁用模拟。
- 启用 Windows 身份验证。
我也有以下代码:
IIdentity ii= Thread.CurrentPrincipal.Identity;
IIdentity iii = Page.User.Identity;
如果我访问该页面,系统会要求我提供 Windows 凭据,我会引入“user2”凭据。
由于模拟被禁用,我希望 IIdentity 名称是“user1”,而不是它的“user2”。
有人可以帮助我了解发生了什么吗?我想我完全误解了“模仿”的概念。
谢谢
更新 1
我搜索了一段时间后发现了这个链接: http://msdn.microsoft.com/en-us/library/aa302377.aspx
似乎有三个 IIdentity 对象。
HttpContext.Current.User.Identity
Thread.CurrentPrincipal.Identity
WindowsIdentity i2 = WindowsIdentity.GetCurrent();
从链接中我了解到:
HttpContext.Current.User.Identity:代表当前请求页面的用户。
Thread.CurrentPrincipal.Identity:当前执行线程的标识。我猜这个身份将是当前 Web 请求运行的身份,它的 asp.net 角色将定义用户在应用程序中可以做什么和不可以做什么。
我想大多数时候 HttpContext.Current.User.Identity 和 Thread.CurrentPrincipal.Identity 都是同一个用户,但在某些情况下,我猜请求页面的用户和 Thread.CurrentPrincipal.Identity 可能不同.
然后是: WindowsIdentity i2 = WindowsIdentity.GetCurrent();
链接显示:“WindowsIdentity = WindowsIdentity.GetCurrent(),它返回当前执行的 Win32 线程的安全上下文的标识。”
在进行了一些启用禁用“模拟”的测试之后,对于我当前的场景,我发现这是被模拟的身份。
如果我不模拟,“WindowsIdentity.GetCurrent();”将反映应用程序池中配置的用户,如果我模拟,身份将更改为我在 web.config 中设置的身份:
<identity impersonate="true" password="**" userName="****" />
更新 2
如果我将 web.config 设置为:
<identity impersonate="true" />
WindowsIdentity.GetCurrent() 被冒充为发出请求的用户,因此:
HttpContext.Current.User.Identity
Thread.CurrentPrincipal.Identity
WindowsIdentity.GetCurrent()
是同一个用户,即请求页面的用户。
【问题讨论】:
标签: asp.net impersonation