c# - How can i remotely access the windows services of a remote host in workgroup computers? -
i writing application used manipulate windows services. computer in workgroup. couldn't access machine. i've tried impersonation using logonuser(). not working. able manipulate using remote desktop connection, couldn't access programatically.
impersonateuser impersonateduser = new impersonateuser(); impersonateduser.impersonate(domain, username, password); servicecontroller sc = new servicecontroller("servicename", host_name); console.writeline("success."); //impersonation. public class impersonateuser { [dllimport("advapi32.dll", setlasterror = true)] public static extern bool logonuser( string lpszusername, string lpszdomain, string lpszpassword, int dwlogontype, int dwlogonprovider, ref intptr phtoken); [dllimport("kernel32.dll", charset = charset.auto)] public extern static bool closehandle(intptr handle); private static intptr tokenhandle = new intptr(0); private static windowsimpersonationcontext impersonateduser; // if incorporate code dll, sure demand // runs fulltrust. [permissionsetattribute(securityaction.demand, name = "fulltrust")] public void impersonate(string domainname, string username, string password) { //try { // use unmanaged logonuser function user token // specified user, domain, , password. const int logon32_provider_default = 0; // passing parameter causes logonuser create primary token. const int logon32_logon_interactive = 2; tokenhandle = intptr.zero; // ---- step - 1 // call logonuser obtain handle access token. bool returnvalue = logonuser( username, domainname, password, logon32_logon_interactive, logon32_provider_default, ref tokenhandle); // tokenhandle - new security token if (false == returnvalue) { int ret = marshal.getlastwin32error(); throw new system.componentmodel.win32exception(ret); } // ---- step - 2 windowsidentity newid = new windowsidentity(tokenhandle); // ---- step - 3 { impersonateduser = newid.impersonate(); } } } // stops impersonation public void undo() { impersonateduser.undo(); // free tokens. if (tokenhandle != intptr.zero) { closehandle(tokenhandle); } } }
i've found cannot impersonate user on workgroup computer. though impersonate, workgrooup computer force user guest. thus, if want login user need change registry value forceguest=0
Comments
Post a Comment