0

I am trying adminstrate a service on a different machine using a ServiceController.

        var sc = new ServiceController(serviceName, machine);
        Console.WriteLine(sc.Status);

As I need to use different credentials, I perform an impersonation using:

        var tokenHandle = IntPtr.Zero;
        bool returnValue = LogonUser(userName, domainName, password,
                                     LOGON32_LOGON_NEW_CREDENTIALS,
                                     LOGON32_PROVIDER_DEFAULT,
                                     ref tokenHandle);            
        if (!returnValue)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
        WindowsIdentity newId = new WindowsIdentity(tokenHandle);
        impersonatedUser = newId.Impersonate();

Impersonation seems to work. But I keep receiving an InvalidOperationException:

System.InvalidOperationException: Cannot open MyService service on computer 'TargetMachine'. 
---> System.ComponentModel.Win32Exception: Access is denied

My Workstation is on a domain, while the target machine participates in a workgroup.

Any idea about what I am missing here?

Jaster
  • 8,255
  • 3
  • 34
  • 60
  • Acces is denied. I had that when I didnt have admin rights.. Run VS in administrator mode. If it works then you just need to give your program admin right. – butterbox Jul 24 '13 at 15:47
  • It works perfectly for local services. I also use [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")] – Jaster Jul 24 '13 at 15:55

1 Answers1

0

My Workstation is on a domain, while the target machine participates in a workgroup

There is your problem. You are impersonating using a local or domain account. They are not part of the target machine since it's in a workgroup.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • I am impersonating into a user which only exists on the workgroup machine. The Impersonation is successful and I also recieve some details regarding the service (Name and description), but nothing else is possible due to "access denied". – Jaster Jul 25 '13 at 20:19