I have a class that needs to know the name of the current user. Environment.UserName or WindowsIdentity.GetCurrent().Name for this. But when impersonation is turned on, they return the name LocalUser not the name ImpersonatedUser .
How to get the name of the current impersonated user?
The application is a C # console application, I know that impersonation is valid, since I get ImpersonatedUser privileges. Of course, I can do the impersonation code to save the given username to some global variable, but that would be wrong.
UPDATE:
Impersonation Code:
if (LogonUser(userName, domain, password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref token) != 0) { if (DuplicateToken(token, 2, ref tokenDuplicate) != 0) { WindowsIdentity tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); _impersonationContext = tempWindowsIdentity.Impersonate();
I have control over the impersonation code, but I would prefer to keep it independent of other parts of the solution.
source share