我正在编写一个类来处理在 asp.net、WCF 服务和 WinForms 应用程序中使用的模拟和委托(delegate)。
根据 MSDN , WindowsIdentity.GetCurrent() 返回代表当前 Windows 用户的 WindowsIdentity 对象。
和
根据 MSDN , WindowsIdentity.Impersonate 允许代码模拟不同的 Windows 用户。
那么,模拟当前用户有什么影响,更重要的是,在 Web 应用程序中,WindowsIdentity.GetCurrent() 如何返回进程启动者身份或已经模拟的最终用户?
最佳答案
如果发生 Win32 错误,
Impersonate()
将抛出一个 SecurityException
。因此,它很可能是通过 Win32 函数实现的,很可能是 ImpersonateLoggedOnUser()。 .
它的文档说(强调我的):
All impersonate functions, including
ImpersonateLoggedOnUser
allow the requested impersonation if one of the following is true:
- The requested impersonation level of the token is less than
SecurityImpersonation
, such asSecurityIdentification
orSecurityAnonymous
.- The caller has the
SeImpersonatePrivilege
privilege.- A process (or another process in the caller's logon session) created the token using explicit credentials through
LogonUser
orLsaLogonUser
function.- The authenticated identity is same as the caller.
因此,我强烈倾向于认为 WindowsIdentity.GetCurrent().Impersonate()
将成功为同一用户建立一个新的模拟层。
关于你问题的第二部分,你似乎混淆了 WindowsIdentity.GetCurrent()
和 HttpContext.User .在 Web 应用程序中,WindowsIdentity.GetCurrent()
始终返回线程所有者(通常是 Network Service
),而 HttpContext.User
返回当前经过身份验证的用户,如果有的话。
关于c# - WindowsIdentity.GetCurrent().Impersonate() 做什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8072624/