【发布时间】:2015-08-11 03:54:32
【问题描述】:
是否可以检查 WindowsIdentity 是否在模拟?
【问题讨论】:
-
我不确定这是否有意义。你想做什么?
标签: c# windows impersonation
是否可以检查 WindowsIdentity 是否在模拟?
【问题讨论】:
标签: c# windows impersonation
是的。只需检查 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);
}
}
输出:
【讨论】: