public static extern bool LoadUserProfile(IntPtr hToken, ref ProfileInfo lpProfileInfo);
public static void DoImpersonation() { WindowsIdentity m_ImpersonatedUser; IntPtr token = IntPtr.Zero; IntPtr tokenDuplicate = IntPtr.Zero; const int SecurityImpersonation = 2; const int TokenType = 1; try { if (RevertToSelf()) { Console.WriteLine("Before impersonation: " + WindowsIdentity.GetCurrent().Name); String userName = "******"; IntPtr password = GetPassword(); if (LogonUser(userName, Environment.MachineName, "!@#$QWERasdf", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0) { if (DuplicateToken(token, SecurityImpersonation, ref tokenDuplicate) != 0) { m_ImpersonatedUser = new WindowsIdentity(tokenDuplicate); using (m_ImpersonationContext = m_ImpersonatedUser.Impersonate()) { if (m_ImpersonationContext != null) { Console.WriteLine("After Impersonation succeeded: " + Environment.NewLine + "User Name: " + WindowsIdentity.GetCurrent(TokenAccessLevels.MaximumAllowed).Name + Environment.NewLine + "SID: " + WindowsIdentity.GetCurrent(TokenAccessLevels.MaximumAllowed).User. Value); #region LoadUserProfile // Load user profile ProfileInfo profileInfo = new ProfileInfo(); profileInfo.dwSize = Marshal.SizeOf(profileInfo); profileInfo.lpUserName = userName; profileInfo.dwFlags = 1; Boolean loadSuccess = LoadUserProfile(tokenDuplicate, ref profileInfo); if (!loadSuccess) { Console.WriteLine("LoadUserProfile() failed with error code: " + Marshal.GetLastWin32Error()); throw new Win32Exception(Marshal.GetLastWin32Error()); } if (profileInfo.hProfile == IntPtr.Zero) { Console.WriteLine( "LoadUserProfile() failed - HKCU handle was not loaded. Error code: " + Marshal.GetLastWin32Error()); throw new Win32Exception(Marshal.GetLastWin32Error()); } #endregion CloseHandle(token); CloseHandle(tokenDuplicate); // Do tasks after impersonating successfully AccessFileSystem(); // Access HKCU after loading user's profile AccessHkcuRegistry(profileInfo.hProfile); // Unload user profile // MSDN remarks http://msdn.microsoft.com/en-us/library/bb762282(VS.85).aspx // Before calling UnloadUserProfile you should ensure that all handles to keys that you have opened in the // user's registry hive are closed. If you do not close all open registry handles, the user's profile fails // to unload. For more information, see Registry Key Security and Access Rights and Registry Hives. UnloadUserProfile(tokenDuplicate, profileInfo.hProfile); // Undo impersonation m_ImpersonationContext.Undo(); } } } else { Console.WriteLine("DuplicateToken() failed with error code: " + Marshal.GetLastWin32Error()); throw new Win32Exception(Marshal.GetLastWin32Error()); } } } } catch (Win32Exception we) { throw we; } catch { throw new Win32Exception(Marshal.GetLastWin32Error()); } finally { if (token != IntPtr.Zero) { CloseHandle(token); } if (tokenDuplicate != IntPtr.Zero) { CloseHandle(tokenDuplicate); } Console.WriteLine("After finished impersonation: " + WindowsIdentity.GetCurrent().Name); } }