Click here to Skip to main content
15,178,202 members
Articles / Desktop Programming / Win32
Posted 5 Nov 2010


42 bookmarked

A Complete Impersonation Demo in C#.NET

Rate me:
Please Sign up or sign in to vote.
4.79/5 (11 votes)
5 Nov 2010CPOL2 min read
This is a complete Impersonation demo in C#, impersonating a user and accessing its files and HKCU Registry entries.


Impersonation is the ability of a thread to execute using a different security information than the process that owns the thread. Typically, a thread in a server application impersonates a client. This allows the server thread to act on behalf of that client to access objects on the server or validate access to the client's own objects.


Under some scenarios, we need to impersonate another Windows account and do some work under that user's session, for example:

  • An enterprise ASP.NET web application provides server administrators' ability to access the server under some specific privilege set; server admin input their NT account information (domain\account + password) on the page, we need to get WinNT Access Token, and then impersonate this server user so that we acquire its specific privilege and do the things only this account can do.
  • We developed a Windows Service which needs internet access periodically, but if a specific user sets an Sock5 proxy to access the internet, then your Windows Service needs to know the Sock5 proxy information so that it could access the internet, and you must impersonate this user and read the settings.


I created a local user TempUser which belongs to "Administrators"(make sure to log on using TempUser at least once). I logged on using my own account and I impersonate TempUser and do two things:

  1. Create a folder C:\TempFolder, modify its default privilege; only TempUser has full control of it. I will create a text file under this folder after impersonating to prove the impersonation is successfull.


    Notes: After setting TempUser as the only owner, my current account cannot access this folder except for privilege promotion (I disable UAC; if UAC is enabled, a prompt window will pop up and ask for Admin confirmation).


    In addition, I try to access this folder programmatically under my account, and an UnauthorizedAccessException is thrown!


  2. I then access TempUser's HKEY_CURRENT_USER and do the Registry key creation, reading, and deleting.

Code Snippet

We need to invoke three very popular Win32 APIs: LogonUser, DuplicateToken, and RevertToSelf.

public static extern int LogonUser(String lpszUserName,
    String lpszDomain,
    String lpszPassword,
    int dwLogonType,
    int dwLogonProvider,
    ref IntPtr phToken);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken, 
       int impersonationLevel, ref IntPtr hNewToken);

/// A process should call the RevertToSelf function after finishing
/// any impersonation begun by using the DdeImpersonateClient,
/// ImpersonateDdeClientWindow, ImpersonateLoggedOnUser,
/// ImpersonateNamedPipeClient, ImpersonateSelf,
/// ImpersonateAnonymousToken or SetThreadToken function.
/// If RevertToSelf fails, your application continues to run in the context
/// of the client, which is not appropriate.
/// You should shut down the process if RevertToSelf fails.
/// RevertToSelf Function:
/// A boolean value indicates the function succeeded or not.
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool CloseHandle(IntPtr handle);

An important note: In order to access HKCU, we need to invoke another Win32 API, LoadUserProfile, to acquire the handle of the HKCU under TempUser. The code below, as the highlighted lines 45 and 49 show, after invoking LoadUserProfile, hProfile will be set as the handle to HKCU:

public struct ProfileInfo
    /// Specifies the size of the structure, in bytes.
    public int dwSize;

    /// This member can be one of the following flags: PI_NOUI or PI_APPLYPOLICY
    public int dwFlags;

    /// Pointer to the name of the user.
    /// This member is used as the base name
    /// of the directory in which to store a new profile.
    public string lpUserName;

    /// Pointer to the roaming user profile path.
    /// If the user does not have a roaming profile, this member can be NULL.
    public string lpProfilePath;

    /// Pointer to the default user profile path. This member can be NULL.
    public string lpDefaultPath;

    /// Pointer to the name of the validating domain controller, in NetBIOS format.
    /// If this member is NULL, the Windows NT 4.0-style policy will not be applied.
    public string lpServerName;

    /// Pointer to the path of the Windows NT 4.0-style policy file. This member can be NULL.
    public string lpPolicyPath;

    /// Handle to the HKEY_CURRENT_USER registry key.
    public IntPtr hProfile;

               SetLastError = true, CharSet = CharSet.Auto)]
    public static extern bool LoadUserProfile(IntPtr hToken, 
                         ref ProfileInfo lpProfileInfo);

       CallingConvention = CallingConvention.Winapi, 
       SetLastError = true, CharSet = CharSet.Auto)]
    public static extern bool UnloadUserProfile(IntPtr hToken, 
                         IntPtr lpProfileInfo);

Code to execute impersonation:

WindowsIdentity m_ImpersonatedUser;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
const int SecurityImpersonation = 2;
const int TokenType = 1;

    if (RevertToSelf())
        Console.WriteLine("Before impersonation: " +

        String userName = "TempUser";
        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: " +
                               TokenAccessLevels.MaximumAllowed).Name +
                            Environment.NewLine +
                            "SID: " +

                        #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: " +
                            throw new Win32Exception(Marshal.GetLastWin32Error());

                        if (profileInfo.hProfile == IntPtr.Zero)
                                "LoadUserProfile() failed - HKCU handle " + 
                                "was not loaded. Error code: " +
                            throw new Win32Exception(Marshal.GetLastWin32Error());


                        // Do tasks after impersonating successfully

                        // Access HKCU after loading user's profile

                        // Unload user profile
                        // MSDN remarks
                        // 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
                Console.WriteLine("DuplicateToken() failed with error code: " + 
                throw new Win32Exception(Marshal.GetLastWin32Error());
catch (Win32Exception we)
    throw we;
    throw new Win32Exception(Marshal.GetLastWin32Error());
    if (token != IntPtr.Zero) CloseHandle(token);
    if (tokenDuplicate != IntPtr.Zero) CloseHandle(tokenDuplicate);

    Console.WriteLine("After finished impersonation: " + 

AccessFileSystem method:

private static void AccessFileSystem()
    // Access file system %appdata% will be "C:\Users\TempUser\appdata\Roaming"
    String appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    File.AppendAllText("C:\\TempFolder\\Temp.txt", "some text...");

AccessHkcuRegistry method:

private static void AccessHkcuRegistry(IntPtr hkcuHandle)
    // Access registry HKCU
    using (SafeRegistryHandle safeHandle = 
              new SafeRegistryHandle(hkcuHandle, true))
        using (RegistryKey tempUserHKCU = RegistryKey.FromHandle(safeHandle))
            // Unum all sub keys under tempuser's HKCU
            String[] keys = tempUserHKCU.GetSubKeyNames();

            // Create a new sub key under tempuser's HKCU
            using (RegistryKey tempKeyByWayne = 
                // Ensure priviledge
                //RegistrySecurity registrySecurity = new RegistrySecurity();
                //RegistryAccessRule accessRule = 
                //   new RegistryAccessRule(Environment.MachineName + "\\" + userName,
                //       RegistryRights.TakeOwnership,
                //       InheritanceFlags.ContainerInherit,
                //       PropagationFlags.None,
                //       AccessControlType.Allow);

                // Create a new String value under created TempKeyByWayne subkey
                     "TempContent", RegistryValueKind.String);

                // Read the value
                using (RegistryKey regKey = 
                    String valueContent = regKey.GetValue("StrType") as String;

                // Delete created TempKeyByWayne subkey

Impersonation result and verification

Temp.txt created


"TempKeyByWayne" created under HKCU



Happy coding :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Wayne Ye
Software Developer (Senior) SAP Labs Shanghai
China China
Wayne is a software developer, Tech Lead and also a geek. He has more than 6 years' experience in Web development(server: ASP.NET (MVC), Web Service, IIS; Client: HTML/CSS/JavaScript/jQuery/AJAX), Windows development (Winform, Windows Service, WPF/Silverlight, Win32 API and WMI) and SQL Server. Deep understanding of GOF Design Patterns, S.O.L.i.D principle, MVC, MVVM, Domain Driven Design, SOA, REST and AOP.

Wayne's Geek Life

Infinite passion on programming!

Comments and Discussions

QuestionThanks, but some questions Pin
marcus187729-Nov-16 20:17
Membermarcus187729-Nov-16 20:17 
GeneralCan't access to different server location Pin
Ahsan Murshed21-Nov-11 20:16
MemberAhsan Murshed21-Nov-11 20:16 
GeneralRe: Can't access to different server location Pin
Wayne Ye24-Nov-11 16:25
MemberWayne Ye24-Nov-11 16:25 
GeneralBig problem Pin
Djordje809-Mar-11 22:45
MemberDjordje809-Mar-11 22:45 
Generalhave 5 Pin
Pranay Rana30-Dec-10 20:37
professionalPranay Rana30-Dec-10 20:37 
GeneralMy vote of 5 Pin
johannesnestler6-Nov-10 3:06
Memberjohannesnestler6-Nov-10 3:06 
GeneralRe: My vote of 5 Pin
Wayne Ye7-Nov-10 2:57
MemberWayne Ye7-Nov-10 2:57 
GeneralRe: My vote of 5 Pin
shakil03040037-Nov-10 4:24
Membershakil03040037-Nov-10 4:24 
General[My vote of 4] Impersonator Pin
King_kLAx5-Nov-10 6:29
MemberKing_kLAx5-Nov-10 6:29 
GeneralRe: [My vote of 4] Impersonator Pin
Wayne Ye5-Nov-10 6:49
MemberWayne Ye5-Nov-10 6:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.