Process.Start() impersonation problem

前端 未结 3 590
名媛妹妹
名媛妹妹 2020-12-06 07:02

Trying to start process with another access token, without success, it runs as the non-impersonated user.

using (WindowsIdentity identity = new WindowsIdenti         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-06 07:57

    Try this example from http://msdn.microsoft.com/en-us/library/w070t6ka.aspx

    private static void ImpersonateIdentity(IntPtr logonToken)
    {
        // Retrieve the Windows identity using the specified token.
        WindowsIdentity windowsIdentity = new WindowsIdentity(logonToken);
    
        // Create a WindowsImpersonationContext object by impersonating the
        // Windows identity.
        WindowsImpersonationContext impersonationContext =
            windowsIdentity.Impersonate();
    
        Console.WriteLine("Name of the identity after impersonation: "
            + WindowsIdentity.GetCurrent().Name + ".");
    
        //Start your process here
        Process.Start("blabla.txt");
    
        Console.WriteLine(windowsIdentity.ImpersonationLevel);
        // Stop impersonating the user.
        impersonationContext.Undo();
    
        // Check the identity name.
        Console.Write("Name of the identity after performing an Undo on the");
        Console.WriteLine(" impersonation: " +
            WindowsIdentity.GetCurrent().Name);
    }
    

    You can also use CreateProcessAsUser windows function.

    http://www.pinvoke.net/default.aspx/advapi32/createprocessasuser.html

提交回复
热议问题