Question : Impersonate a logged on user

I have a service that is running as a local system account.  I need to get access to a the logg on users registry.  I have tried to impersonate the logged on user but no luck.  It appears to change the impersonation, but when I access the registry it is still the services.  Anyone have any ideas?

I tried getting the users token like this using the GetLoggedOnUserToken function below.

{
                    System.IntPtr userToken = GetLoggedOnUserToken();

                        pLog.InfoFormat( "Name of the identity before impersonation: {0}.", WindowsIdentity.GetCurrent().Name );
                    ImpersonateLoggedOnUser( userToken );
                        pLog.InfoFormat( "Name of the identity after impersonation: {0}.", WindowsIdentity.GetCurrent().Name );
                Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Test");
                sSessionId = Convert.ToString(key.GetValue("Session"));
               
                   RevertToSelf();
}

    public static IntPtr GetLoggedOnUserToken( )
    {
        IntPtr primaryToken = IntPtr.Zero;

        //Either specify the processID explicitly
        //Or try to get it from a process owned by the user.
        //In this case assuming there is only one explorer.exe

        Process[] ps = Process.GetProcessesByName("explorer");
        int processId = -1;//=processId
        if (ps.Length > 0)
        {
            processId = ps[0].Id;
        }

        if (processId > 1)
        {
            IntPtr token = IntPtr.Zero;
            bool retVal = false;
            Process p = null;

            try
            {
                p = Process.GetProcessById(processId);
            }

            catch (ArgumentException)
            {

                string details = String.Format("ProcessID {0} Not Available", processId);
                Debug.WriteLine(details);
                throw;
            }

            //Gets impersonation token
            retVal = OpenProcessToken(p.Handle, TOKEN_ALL_ACCESS, ref token);
            if (retVal == true)
            {

                SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
                sa.nLength = (uint)Marshal.SizeOf(sa);

                //Convert the impersonation token into Primary token
                retVal = DuplicateTokenEx(
                token,
                TOKEN_ALL_ACCESS,
                ref sa,
                (int)ProcessUtility.SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation,
                (int)ProcessUtility.TOKEN_TYPE.TokenImpersonation,
                ref primaryToken);

                //Close the Token that was previously opened.
                CloseHandle(token);
                if (retVal == false)
                {
                    string message = String.Format("DuplicateTokenEx Error: {0}", Marshal.GetLastWin32Error());
                    Debug.WriteLine(message);
                }
            }
            else
            {
                string message = String.Format("OpenProcessToken Error: {0}", Marshal.GetLastWin32Error());
                Debug.WriteLine(message);

            }
        }
        return primaryToken;
    }

Answer : Impersonate a logged on user

Hello fitaliano,

Click any of the existing labels, then click Tools - XY Chart Labels - Add chart labels. Click in the Label Range field and select cells I2:I44. Set the label position to Center and click OK.

See attached for the result.

cheers, teylyn
Random Solutions  
 
programming4us programming4us