![]() ![]() Throw new Exception("LogonUser failed with error code : " GetError(ret)) īool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle) LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_WINNT50, ref tokenHandle) Call LogonUser to obtain a handle to an access tokenīool returnValue = LogonUser(svcUserName, domainName, password, Public static WindowsImpersonationContext doImpersonation(string svcUserName, string domainName, string password) Private static IntPtr dupeTokenHandle = new IntPtr(0) Private static IntPtr tokenHandle = new IntPtr(0) Declare the Impersonation Levels as constants Declare the Logon Providers as constants Public extern static bool CloseHandle(IntPtr handle) Ĭonst int LOGON32_LOGON_NETWORK_CLEARTEXT = 8 // Win2K or higherĬonst int LOGON32_LOGON_NEW_CREDENTIALS = 9 // Win2K or higher Int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle) Public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, Both return a WindowsImpersonationContext. It has 2 main entry points, getWic() and doImpersonation() - getWic() will take a username that looks like domain\user or machinename\user and split them up into their component parts before handing it off to doImpersonation(), while doImpersonation() accepts the parts already split, if you have it like that and don't need the code in getWic(). if (tokenHandle != IntPtr.Zero) CloseHandle(tokenHandle) The above line just basically does this on the tokens. WindowsImpersonationContext adminContext = Impersonation.getWic(userName, password) Ĭ("\nUnable to set profile to Mandatory:\n\t" ex.Message) if brought in by a text box, it would be just domain\user but you have to escape it if hard-coding. Otherwise, as it has been mentioned in another comment, you would likely have to set this up with impersonation if the account you are running under does not have admin privileges on that machine, like so: string userName = "domain\\user" // there's really just one slash, Sc.Status.Equals(ServiceControllerStatus.StopPending)) If (sc.Status.Equals(ServiceControllerStatus.Stopped) || ServiceController sc = new ServiceController("YourService", "MachineName") If you DO have admin privileges on that machine, this code will work perfectly fine: using System.ServiceProcess ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |