Click here to Skip to main content
15,914,323 members
Home / Discussions / C#
   

C#

 
GeneralRe: loading a bitmap file Pin
yueue3-Aug-06 0:24
yueue3-Aug-06 0:24 
JokeRe: loading a bitmap file Pin
mav.northwind3-Aug-06 1:04
mav.northwind3-Aug-06 1:04 
GeneralRe: loading a bitmap file Pin
yueue3-Aug-06 2:57
yueue3-Aug-06 2:57 
GeneralRobert I need some help Pin
diddy343-Aug-06 1:18
diddy343-Aug-06 1:18 
GeneralRe: Robert I need some help Pin
Christian Graus3-Aug-06 1:32
protectorChristian Graus3-Aug-06 1:32 
GeneralRe: Robert I need some help [modified] Pin
Dave Kreskowiak3-Aug-06 1:48
mveDave Kreskowiak3-Aug-06 1:48 
GeneralRe: Robert I need some help Pin
stancrm3-Aug-06 19:57
stancrm3-Aug-06 19:57 
Questionservice controller/impersonate error [modified] Pin
JakeFront3-Aug-06 0:03
professionalJakeFront3-Aug-06 0:03 
i am connecting to a different domain to access the service control manager using the service controller and the impersonate LogonUser. I can connect to the domains but on certain machines i get the error Cannot open Service Control Manager on xxxxx. This operation might require other priviliges.

I have logged onto the machine to see the priviliges and they are fine. The login i am using has the act as part of operating system.

i am using xp on the source machine but the target machine is using windows 2000.

The code for the impersonater is the following

public class ImpersonateUser<br />
   {<br />
      [DllImport("advapi32.dll", SetLastError = true)]<br />
      public static extern bool LogonUser(<br />
          String lpszUsername,<br />
          String lpszDomain,<br />
          String lpszPassword,<br />
          int dwLogonType,<br />
          int dwLogonProvider,<br />
          ref IntPtr phToken);<br />
<br />
      [DllImport("kernel32.dll", CharSet = CharSet.Auto)]<br />
      public extern static bool CloseHandle(IntPtr handle);<br />
<br />
      private static IntPtr tokenHandle = new IntPtr(0);<br />
      private static WindowsImpersonationContext impersonatedUser;<br />
<br />
      // If you incorporate this code into a DLL, be sure to demand that it<br />
      // runs with FullTrust.<br />
      [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]<br />
      public void Impersonate(string domainName, string userName, string password) {<br />
         try {<br />
<br />
            // Use the unmanaged LogonUser function to get the user token for<br />
            // the specified user, domain, and password.<br />
            // Xc: ---- For const descriptions see<br />
            // ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.WIN32COM.v10.en/secauthn/security/logonuser.htm<br />
            //const int LOGON32_PROVIDER_DEFAULT = 0;<br />
			const int LOGON32_PROVIDER_WINNT50 = 3;<br />
			const int LOGON32_LOGON_NEW_CREDENTIALS = 9;<br />
		    //const int LOGON32_LOGON_SERVICE = 5;<br />
            // Passing this parameter causes LogonUser to create a primary token.<br />
            //const int LOGON32_LOGON_INTERACTIVE = 2;<br />
            //const int LOGON32_LOGON_NETWORK = 3; <br />
            tokenHandle = IntPtr.Zero;<br />
            <br />
            // Call  LogonUser to obtain a handle to an access token.<br />
            // Xc: ---- LogonUser documentation<br />
            // ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.WIN32COM.v10.en/secauthn/security/logonuser.htm<br />
            bool returnValue = LogonUser(<br />
                userName,<br />
                domainName,<br />
                password,<br />
                LOGON32_LOGON_NEW_CREDENTIALS, // Xc:-- will cache logon information for disconnected operations<br />
                /*LOGON32_LOGON_NETWORK, // Xc:-- This type of access is faster */<br />
                //LOGON32_PROVIDER_DEFAULT,<br />
				LOGON32_PROVIDER_WINNT50,<br />
                ref tokenHandle);         // tokenHandle - new security token<br />
<br />
<br />
            if (false == returnValue) {<br />
               int ret = Marshal.GetLastWin32Error();<br />
               Console.WriteLine("LogonUser call failed with error code : " +<br />
                   ret);<br />
               throw new System.ComponentModel.Win32Exception(ret);<br />
            }<br />
<br />
            // Xc: ---- Assume new identity<br />
            WindowsIdentity newId = new WindowsIdentity(tokenHandle);<br />
            impersonatedUser = newId.Impersonate();<br />
<br />
         }<br />
         catch (Exception ex) {<br />
            Console.WriteLine("Exception occurred. " + ex.Message);<br />
			 return;<br />
         }<br />
      }<br />
<br />
      // Stops impersonation<br />
      public void Undo() {<br />
         impersonatedUser.Undo();<br />
         // Free the tokens.<br />
         if (tokenHandle != IntPtr.Zero)<br />
            CloseHandle(tokenHandle);<br />
      }

i have used every combination of LOGON32 and the PROVIDER.

The service controller code is as follows
ImpersonateUser iU = new ImpersonateUser();<br />
<br />
				iU.Impersonate(DomainName, UserName, Password);<br />
				System.ServiceProcess.ServiceController  [] myController1;<br />
				myController1 = System.ServiceProcess.ServiceController.GetServices("10.8.2.120");<br />
<br />
				foreach( System.ServiceProcess.ServiceController controller in myController1 )<br />
				{<br />
<br />
					MessageBox.Show(controller.ServiceName);<br />
				}

the message box is just to test if i can access the service control manager.

The error i think is from the service controller. Can anyone help?Confused | :confused:


-- modified at 12:32 Thursday 3rd August, 2006

Rick

QuestionDelegates Pin
fmardani2-Aug-06 23:48
fmardani2-Aug-06 23:48 
AnswerRe: Delegates Pin
Christian Graus3-Aug-06 0:01
protectorChristian Graus3-Aug-06 0:01 
GeneralRe: Delegates Pin
yueue3-Aug-06 0:32
yueue3-Aug-06 0:32 
AnswerRe: Delegates Pin
NasimKaziS3-Aug-06 1:51
NasimKaziS3-Aug-06 1:51 
QuestionExposing classes' methods/properties in Web Service [modified] Pin
impeham2-Aug-06 23:29
impeham2-Aug-06 23:29 
AnswerRe: Exposing classes' methods/properties in Web Service [modified] Pin
sundar1563-Aug-06 11:26
sundar1563-Aug-06 11:26 
GeneralRe: Exposing classes' methods/properties in Web Service Pin
impeham4-Aug-06 5:23
impeham4-Aug-06 5:23 
QuestionSysetm.Collections.Hashtable Pin
El'Cachubrey2-Aug-06 23:08
El'Cachubrey2-Aug-06 23:08 
AnswerRe: Sysetm.Collections.Hashtable Pin
Christian Graus2-Aug-06 23:10
protectorChristian Graus2-Aug-06 23:10 
GeneralRe: Sysetm.Collections.Hashtable [modified] Pin
El'Cachubrey2-Aug-06 23:21
El'Cachubrey2-Aug-06 23:21 
GeneralRe: Sysetm.Collections.Hashtable Pin
Guffa2-Aug-06 23:43
Guffa2-Aug-06 23:43 
GeneralRe: Sysetm.Collections.Hashtable [modified] Pin
El'Cachubrey2-Aug-06 23:50
El'Cachubrey2-Aug-06 23:50 
AnswerRe: Sysetm.Collections.Hashtable Pin
stancrm2-Aug-06 23:20
stancrm2-Aug-06 23:20 
GeneralRe: Sysetm.Collections.Hashtable Pin
El'Cachubrey2-Aug-06 23:22
El'Cachubrey2-Aug-06 23:22 
Questionconnection pro Pin
skyeddie2-Aug-06 23:06
skyeddie2-Aug-06 23:06 
AnswerRe: connection pro Pin
Christian Graus2-Aug-06 23:11
protectorChristian Graus2-Aug-06 23:11 
GeneralRe: connection pro Pin
skyeddie2-Aug-06 23:25
skyeddie2-Aug-06 23:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.