ASP.NET調用cmd命令提示字元拒絕訪問解決方案

來源:互聯網
上載者:User

標籤:代碼   input   提示符   readline   director   append   blank   style   系統使用者   

   最近做關於Windows Server POP3伺服器的小項目,翻遍網路,也沒能找到介面。

   值得慶幸的是最終發現了一個控制台介面winpop.exe,也就是用cmd命令提示字元與POP3伺服器互動。

    這樣實屬無奈之舉,用cmd命令效率低不說,而且擷取、分析返回資訊相當麻煩。然而這還不算什麼,最主要的是:B/S模式下,網頁程式有許可權調用cmd嗎?
   這裡調用cmd,當然不是調用客戶機器上的cmd,而是伺服器上的cmd,這樣心裡還有點底。

   小心翼翼的在我電腦上做了實驗,成功在aspx頁面中調用cmd,並執行了ping命令,而且順利擷取、分析返回資訊。

   於是我滿懷信心的把測試程式發布到了伺服器上,也是順利執行!核心代碼如下:

using System.Diagnostics;  using System.IO;        /// <summary>  /// cmd命令執行助手類  /// </summary>  public class CMDHelper  {      private Process p = null;      private StreamReader reader = null;        /// <summary>      /// 通過cmd執行ping命令      /// </summary>      /// <param name="cmdString">命令內容</param>      /// <returns>命令執行結果</returns>      public string execute(string cmdString)       {          ProcessStartInfo start = new ProcessStartInfo("cmd.exe");            start.FileName = "ping";          start.Arguments = cmdString;            start.CreateNoWindow = true;            start.RedirectStandardOutput = true;            start.RedirectStandardInput = true;            start.UseShellExecute = false;            start.WorkingDirectory = "C:\\WINDOWS\\system32";            p = Process.Start(start);            reader = p.StandardOutput;            string line = "";          string lineTemp = "";             //逐行讀出返回的資料,並做一定處理,方便解析          while (!reader.EndOfStream)          {              lineTemp = reader.ReadLine();//讀出一行              //將空行用*號表示              if (lineTemp == "")              {                  line += "*";              }              else              {                  line += lineTemp;              }          }            return line;      }  }  

       可是高興的太早了,當我把start.FileName= "ping";改成start.FileName = "winpop";,也就是執行winpop命令,這時候無情的返回了“拒絕訪問”。

         分析一下,是什麼拒絕訪問了?剛剛已經成功執行了ping,說明訪問cmd的許可權是有的,執行的ping命令實際上是利用cmd.exe調用了ping.exe,這說明訪問ping.exe的許可權也是有的。而現在換成winpop命令,卻出現了拒絕訪問,說明我們沒有許可權訪問winpop.exe!

         這下該怎麼辦呢?首先想到的就是修改winpop.exe的存取權限,右擊winpop.exe(在Windows/System32檔案夾下),點擊【屬性】---【安全】選項卡,在使用者中加入目前使用者,還是拒絕;加入ASP.NET使用者,還是拒絕;最後急了,加上Everyone使用者,依然拒絕!看來這種方法沒有作用。

         再冷靜的分析一下,這肯定是許可權問題引起的。那麼這個許可權究竟在哪限制了?

別忘了,我們的程式是運行在IIS中的,所有的生殺大權,都由IIS掌握,許可權是不是在這裡呢?

         果斷google一下,IIS還真有許可權,更確切的說,是IIS中的“應用程式集區”可以設定許可權。接下來就說說如何設定。

         先從IIS中找到你的網站,在右鍵--【屬性】中看看使用的應用程式集區是哪個,然後【在應用程式集區】目錄下找到它,右鍵---【屬性】

 

         找到【標識】選項卡,再找到【預定義賬戶】,在後邊的下拉式功能表中選擇“本地系統”就可以了!

 

         這樣一來,你的網站就可以隨心所欲的執行cmd命令了,其實不僅僅是執行cmd命令,簡直是至高無上的許可權!

         提醒一下,這樣更改的是應用程式集區許可權,因此所有使用這個應用程式集區的網站都有很高的許可權,這是相當危險的,還須謹慎使用!!

         作為完整的解決方案,還需要提一點。

         調用cmd執行命令時,可以指定使用者(使用者是指系統使用者,也就是登入電腦時用的帳號密碼),這樣也可以獲得一定的許可權。代碼如下:

using System.Diagnostics;  using System.IO;    /// <summary>  /// cmd命令執行助手類  /// </summary>  public class CMDHelper  {      private Process p = null;      private StreamReader reader = null;        /// <summary>      /// 通過cmd執行ping命令      /// </summary>      /// <param name="cmdString">命令內容</param>      /// <returns>命令執行結果</returns>      public string execute(string cmdString)       {          ProcessStartInfo start = new ProcessStartInfo("cmd.exe");            start.FileName = "ping";          start.Arguments = cmdString;            start.CreateNoWindow = true;            start.RedirectStandardOutput = true;            start.RedirectStandardInput = true;            start.UseShellExecute = false;            start.WorkingDirectory = "C:\\WINDOWS\\system32";                    start.UserName = "administrator"; //指定使用者          //構造使用者密碼,假定密碼為123,必須一個字元一個字元的添加          System.Security.SecureString password = new System.Security.SecureString();          password.AppendChar(‘1‘);          password.AppendChar(‘2‘);          password.AppendChar(‘3‘);          start.Password = password;                    p = Process.Start(start);            reader = p.StandardOutput;            string line = "";          string lineTemp = "";             //逐行讀出返回的資料,並做一定處理,方便解析          while (!reader.EndOfStream)          {              lineTemp = reader.ReadLine();//讀出一行              //將空行用*號表示              if (lineTemp == "")              {                  line += "*";              }              else              {                  line += lineTemp;              }          }            return line;      }  }  

   但是本人在Windows Server2003下測試失敗,只要指定使用者,網頁程式就掛起,直到響應逾時,原因未知。

 

ASP.NET調用cmd命令提示字元拒絕訪問解決方案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.