c# - WindowsIdentity.GetCurrent().Impersonate() 做什么

标签 c# .net

我正在编写一个类来处理在 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 as SecurityIdentification or SecurityAnonymous.
  • 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 or LsaLogonUser 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/

相关文章:

c# - .NET 中的 JIT 编译器和事件处理程序

c# - AppDomain 之间的通信

c# - 使用 XMLDataProvider 作为静态资源将数据绑定(bind)到 ComboBox WPF

c# - 指向 PointCollection 的字符串

c# - 使用从不同程序集中的类继承的模型的代码优先方法

c# - 如何使用 LINQ 从另一个列表填充对象的属性

c# - DevExpress Gridview - 根据特定条件格式化 C# 中的行

.net - VB.net Mid 作为左运算符特殊(奇怪?)行为

c# - C# 中 foreach() 的复杂性。网

c# - 在 VSTO 中获取 Excel 工作簿的哈希码以启用基于状态的按钮