#Region Using Directives Using System; Using System.Security.principal; Using System.Runtime.InteropServices; Using System.Text.RegularExpressions; #ENDREGON
A summary description of Namespace TestenumWindows {///
[DLLIMPORT ("kernel32.dll", charset = charset.auto)] public extern static bool closehandle (INTPTR HANDLE);
[DllImport ( "advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public extern static bool DuplicateToken (IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
Public static void main (string [] args) {console.writeline ("The current user is:" windowsidentity.getCurrent (); impersonatedWork testdel = new impersonateDwork (test); impersonateAnddo ("test", "," BCA ~ 123 ", testdel); console.writeline (" The current user is: " windowsidentity.getcurrent (). name);}
Static void test () {console.writeline ("The current user is:" windowsidentity.getcurrent (). name);
Public delegate void impersonatedWork ();
bool retVal = DuplicateToken (tokenHandle, SecurityImpersonation, ref dupeTokenHandle); if (retVal!) {CloseHandle (tokenHandle); throw new ApplicationException ( "copy Handle wrong!");} WindowsIdentity newId = new WindowsIdentity (dupeTokenHandle); WindowsImpersonationContext impersonatedUser = Newid.impersonate (); whattodo (); impersonateduser.undo (); if (tokenhandle! = INTPTR.ZERO) CloseHandle; if (DupetokenHandle! = INTPTR.ZERO) CloseHandle (Dupetokenhandle);}}}