2016-05-23 10 views
0

昇格したコマンドプロンプトプロセスを実行します。私はユーザーがいる:myDomain \ myAdminとmyDomain \ myUser。昇格したコマンドプロンプトプロセスの実行

myDomain \ myAdminの下に次のコードを実行すると正常に動作します。しかし、myDomain \ myUserの次の例外が表示されます: "Unknown error(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); 
     } 
    } 
} 
+0

コードをステップ実行したときにデバッガからあなたに通知される内容は何ですか? –

+0

好奇心を要して、なぜあなたは管理者アカウントにログインしようとしていますか? 'myUser'は管理者権限でアプリケーションを実行するためにパスワードが必要ですか?もしそうなら、代わりに 'CreateProcessAsUser()'を試してみてください。 –

+0

あなたが記録したメッセージの完全なリストのような詳細を提供できるといいですね。または例外が発生した場所。 'Process.Start()'へのあなたの呼び出しが失敗したようです。 – user314104

答えて

0

私はCreateProcessWithLogonW関数でこれを行いました - ありがとうございます。

関連する問題