获取模拟的用户名 [英] Get impersonated user name
问题描述
予有需要知道当前有效的用户的名称的类。 Environment.UserName
或 WindowsIdentity.GetCurrent()。名称
为。但是,当启用模拟,他们返回 LocalUser上
的名称不是 ImpersonatedUser
的名字。
I have a class that needs to know name of a user currently in effect. Environment.UserName
or WindowsIdentity.GetCurrent().Name
is for that. But when impersonation is enabled, they return LocalUser
name not the ImpersonatedUser
name.
如何获得当前模拟的用户名?
How to get name of currently impersonated user?
该应用程序是C#控制台应用程序,我知道,模拟实际上是因为我得到 ImpersonatedUser
的priviledges。当然,我可以模拟code冒充用户名保存到一些全局变量,但它是错误的。
The app is C# console application, I know that impersonation is in effect since I get priviledges of ImpersonatedUser
. Sure I can make impersonation code save impersonated username to some global variable, but it would be wrong.
更新:
模拟code:
if (LogonUser(userName, domain, password, LOGON32_LOGON_NEW_CREDENTIALS/*=9*/, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
WindowsIdentity tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
_impersonationContext = tempWindowsIdentity.Impersonate();
// WindowsIdentity.GetCurrent().Name equals "LocalUser"
// while userName equals "ImpersonatedUser"
...
我有超过模拟code控制,但我会preFER,以保持它的解决方案,其他地区独立。
I have control over impersonation code, but I would prefer to keep it independant from other parts of solution.
推荐答案
好吧,看来问题是在PROPERT非人格化的登录类型。
Ok, it appears that problem was in propert impersonalization logon type.
如果在非人格化code替换 LOGON32_LOGON_NEW_CREDENTIALS
(9) LOGON32_LOGON_INTERACTIVE
(2)一切工作正常 - WindowsIdentity.GetCurrent()。名称
和 Environment.UserName
都返回ImpersonatedUser预期。
If in impersonalization code replace LOGON32_LOGON_NEW_CREDENTIALS
(9) with LOGON32_LOGON_INTERACTIVE
(2) everything works fine - WindowsIdentity.GetCurrent().Name
and Environment.UserName
both return ImpersonatedUser as expected.
这篇关于获取模拟的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!