cubic83
Goto Top

C Sharp - Prozess als Administrator laufen lassen

Hallo,

ich starte ein Programm als User und möchte gerne ein weiteres Programm als Administrator starten.

Versuch 1:

Process proc = new Process();
proc.StartInfo.FileName = @"C:\myprogram.exe";  
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Arguments = "";  
proc.StartInfo.UserName = "Administrator";  
System.Security.SecureString secpass = new System.Security.SecureString();

secpass.AppendChar('m');  
secpass.AppendChar('y');  
secpass.AppendChar('p');  
secpass.AppendChar('a');  
secpass.AppendChar('s');  
secpass.AppendChar('s');  

proc.StartInfo.Password = secpass;
proc.StartInfo.Domain = Environment.MachineName;
proc.StartInfo.WorkingDirectory = System.Environment.CurrentDirectory;

proc.Start();         

Exception: Access is denied

Versuch 2

public void RunProgram()
{

   ImpersonateUser("adminstrator", "mypass", System.Environement.MachineName);  
   WindowsIdentity identity = WindowsIdentity.GetCurrent();
   WindowsPrincipal principal = new WindowsPrincipal(identity);


    if (principal.IsInRole(WindowsBuiltInRole.Administrator))
    {
        MessageBox.Show("Running as Administrator");  
        Process proc = new Process();
        proc.StartInfo.FileName = @"C:\myprogram.exe";  
        proc.StartInfo.Arguments = "";  
        proc.StartInfo.WorkingDirectory = System.Environment.CurrentDirectory;
        proc.Start();         
     }

}

    public enum SECURITY_IMPERSONATION_LEVEL : int
    {
        SecurityAnonymous = 0,
        SecurityIdentification = 1,
        SecurityImpersonation = 2,
        SecurityDelegation = 3
    }


        [DllImport("advapi32.dll", SetLastError = true)]  
        public static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

        [DllImport("kernel32")]  
        private static extern int CloseHandle(IntPtr handle);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
        public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);

        static public void ImpersonateUser(string sUsername, string sDomain, string sPassword)
        {
            IntPtr pExistingTokenHandle = new IntPtr(0);
            IntPtr pDuplicateTokenHandle = new IntPtr(0);
            pExistingTokenHandle = IntPtr.Zero;
            pDuplicateTokenHandle = IntPtr.Zero;

            try
            {
                const int LOGON32_PROVIDER_DEFAULT = 0;
                const int LOGON32_LOGON_INTERACTIVE = 2;
                bool bImpersonated = LogonUser(sUsername, sDomain, sPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,ref pExistingTokenHandle);

                
                if (false == bImpersonated)
                {
                    int nErrorCode = Marshal.GetLastWin32Error();
                }
                
                bool bRetVal = DuplicateToken(pExistingTokenHandle, (int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, ref pDuplicateTokenHandle);
                
                if (false == bRetVal)
                {
                    int nErrorCode = Marshal.GetLastWin32Error();
                    CloseHandle(pExistingTokenHandle);
                }
                else
                {
                    WindowsIdentity newId = new WindowsIdentity(pDuplicateTokenHandle);
                    WindowsImpersonationContext impersonatedUser = newId.Impersonate();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Funktionniert zwar - der Prozess läuft aber im Userkontext.


Kann mir bitte jemand sagen was ich falsch mache oder wie ich es richtig machen muss? Google ist voll von Tipps - es läuft aber auf diese beiden Varianten hinaus. Das Programm läuft unter Windows XP mit .NET 3.5 installiert.

Vielen Dank und ein schönes Wochenende

Content-Key: 175075

Url: https://administrator.de/contentid/175075

Printed on: April 18, 2024 at 16:04 o'clock

Member: vorstieg
vorstieg Oct 21, 2011 at 13:38:54 (UTC)
Goto Top
Hallo, folgender Code funktioniert bei mir einwandfrei:

Process prog = new Process();
prog.StartInfo.UserName=@"Administrator";
System.Security.SecureString secpass = new System.Security.SecureString();
secpass.AppendChar('p');
secpass.AppendChar('a');
secpass.AppendChar('s');
secpass.AppendChar('s');
prog.StartInfo.Password = secpass;
prog.StartInfo.UseShellExecute = false;
prog.StartInfo.FileName = @"C:\\folder\test.exe";
prog.Start();


Bei welcher Zeile kommt denn die Fehlermeldung bei Version 1?

mfg Flo
Member: Cubic83
Cubic83 Oct 21, 2011 at 16:10:01 (UTC)
Goto Top
Dieser Code klappt aber leider nicht.

Der Fehler wird bei proc.Start() geworfen.