运行提升的命令提示符进程

时间:2016-05-23 18:06:17

标签: c#

我想运行一个提升的命令提示符进程。我有用户:myDomain \ myAdmin和myDomain \ myUser。

如果我在myDomain \ myAdmin下运行下一个代码,它可以正常工作。但是在myDomain \ myUser下,出现下一个异常:“未知错误(0xfffffffe)”。 有什么想法吗?

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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我用CreateProcessWithLogonW函数做了 - 谢谢。