【问题标题】:How to check if a Windows Identity is impersonating?如何检查 Windows 身份是否在模拟?
【发布时间】:2015-08-11 03:54:32
【问题描述】:

是否可以检查 WindowsIdentity 是否在模拟?

【问题讨论】:

  • 我不确定这是否有意义。你想做什么?

标签: c# windows impersonation


【解决方案1】:

是的。只需检查 WindowsIdentity 类的 ImpersonationLevel 属性即可。

来自 MSDN:

获取用户的模拟级别

  • 匿名 - 服务器进程无法获取客户端的标识信息,也无法冒充客户端
  • 委托 - 服务器进程可以在远程系统上模拟客户端的安全上下文
  • 标识 - 服务器进程可以获取有关客户端的信息...
  • 模拟 - 服务器进程可以在其本地系统上模拟客户端的安全上下文。

代码sn-p(修改MSDN example):

var identity = WindowsIdentity.GetCurrent();
Console.WriteLine("Before impersonation: " + identity.Name);
Console.WriteLine("ImpersonationLevel: {0}", identity.ImpersonationLevel);

// Use the token handle returned by LogonUser. 
using (WindowsIdentity newId = new   
       WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
{
    using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
    {

        // Check the identity.
        identity = WindowsIdentity.GetCurrent();
        Console.WriteLine("After impersonation: "+ identity.Name);
        Console.WriteLine("ImpersonationLevel: {0}", identity.ImpersonationLevel);
    }
}

输出:

更多

Tell me more

【讨论】:

  • 谢谢,我想会的。
猜你喜欢
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-30
  • 2017-12-04
  • 1970-01-01
相关资源
最近更新 更多