I am trying to start and stop services on a remote machine in a different domain.
I can logon to different domains and start and stop services but this particluar domain i can logon on ok, using the impersonate function in C#, but keeps returning with an error
"Cannot open service control manager on computer 'IPAddress'. This operation may require other priviliges".
I know the computer is on and the logon i am using is administrator. Can anyone help please.
Loading
Krishnaraj LPosted Aug 25, 2006, 5:25 AM
It is working fine for me. Can you please verify whether the flag you have used is correct and you have created same username and password in both the machines? What i meant was the machine where the application runs and the machine which you are trying to connect should have the same user name (local admin) and password. Instead of domain name you should give your machine name.
Please try it out and let me know.
leoPosted Aug 21, 2006, 2:16 AM
Rick PatelPosted Aug 14, 2006, 9:22 AM
Thanks for the input, i already have this in my code as a seperate class.
I changed some of the code to match your as i didn't have a duplicate token in my code and i was using the new credentials = 9 which was conecting to the server but not allowing me to access the service control manager.
I have tried every combination of the LOGON32_LOGON but to no avail. I will keep trying.
Krishnaraj LPosted Aug 7, 2006, 1:00 PM
Rick,
Well, as per microsoft documentation 'You cannot use LogonUser to log on to a remote computerYou cannot use LogonUser to log on to a remote computer' (I just copied this from MSDN).
Now to solve the problem, i tried something like this.
Created a local admin user in my machine and i created the same user in the remote machine as well, from which i am trying to retrieve the service names. Then i called LogonUser by passing this user name/Password/machine name (not domain name). Then i used this token to create a windows identity, then i called impersonate function. Have a look at the code:
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);
string strUserName, strMachineName, strPassword;
strUserName = "USER";
strMachineName = "MACHINE_NAME";
strPassword = "Password";
const int LOGON32_PROVIDER_DEFAULT = 0; const int LOGON32_LOGON_INTERACTIVE = 2; // This flag is important const int SecurityImpersonation = 2;tokenHandle = IntPtr.Zero;
dupeTokenHandle = IntPtr.Zero;
bool returnValue = LogonUser(strUserName, strMachineName, strPassword,LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle); string sObj = tokenHandle.ToString(); bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle); if (false == retVal){
CloseHandle(tokenHandle);
return;}
WindowsIdentity newId =
new WindowsIdentity(dupeTokenHandle);WindowsImpersonationContext impersonatedUser = newId.Impersonate();
MessageBox.Show (WindowsIdentity.GetCurrent().Name );
arrServices = ServiceController.GetServices ("xxx.xxx.xxx.xxx"); // IP of the remote machine--------
Try this and let me know.