Run an elevated command prompt process

0

I want to run an elevated command prompt process. I have users: myDomain\myAdmin and myDomain\myUser.

If I run next code under myDomain\myAdmin, it works fine. But under myDomain\myUser, next exception appear: "Unknown error (0xfffffffe)". Any ideas why?

namespace myProcess
{
    public partial class Form1 : Form
    {
        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
        int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public extern static bool CloseHandle(IntPtr handle);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SafeTokenHandle safeTokenHandle;
                string userName = "myAdmin", domainName = "myDomain", password = "Password";

                const int LOGON32_PROVIDER_DEFAULT = 0;
                //This parameter causes LogonUser to create a primary token. 
                const int LOGON32_LOGON_INTERACTIVE = 2;

                // Call LogonUser to obtain a handle to an access token. 
                bool returnValue = LogonUser(userName, domainName, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle);


                if (returnValue)
                {
                    using (safeTokenHandle)
                    {

                        txtLogs.Text += "\r\nBefore: " + WindowsIdentity.GetCurrent().Name;

                        // Use the token handle returned by LogonUser. 
                        using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
                        {
                            using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
                            {
                                txtLogs.Text += "\r\nImpersonation: " + WindowsIdentity.GetCurrent().Name;
                                RunProcess();
                            }
                        }

                        // Releasing the context object stops the impersonation - check the identity.
                        txtLogs.Text += "\r\nAfter: " + WindowsIdentity.GetCurrent().Name;

                    }
                }
                else
                {
                    int ret = Marshal.GetLastWin32Error();
                    txtLogs.Text += "\r\nLogonUser failed with error code: " + ret;
                }

            }
            catch (Exception ex)
            {
                txtLogs.Text += "\r\nMAIN: " + ex.Message;
            }
        }

        private void RunProcess()
        {
            try
            {
                ProcessStartInfo proc = new ProcessStartInfo();
                proc.UseShellExecute = true;
                proc.FileName = "cmd.exe";
                proc.Verb = "runas";
                proc.LoadUserProfile = true;

                Process p = Process.Start(proc);
            }
            catch (Exception ex)
            {
                txtLogs.Text += "\r\n" + ex.Message;
            }
        }

    }

    public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
    {
        private SafeTokenHandle() : base(true)
        {
        }

        [DllImport("kernel32.dll")]
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        [SuppressUnmanagedCodeSecurity]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool CloseHandle(IntPtr handle);

        protected override bool ReleaseHandle()
        {
            return CloseHandle(handle);
        }
    }
}
c#
asked on Stack Overflow May 23, 2016 by Stoica Leonard

1 Answer

0

I did it with CreateProcessWithLogonW function - thank's.

answered on Stack Overflow May 24, 2016 by Stoica Leonard

User contributions licensed under CC BY-SA 3.0