通過ASP.NET頁面重啟伺服器

來源:互聯網
上載者:User
asp.net|伺服器|頁面

最近在設計網站後台管理系統的時候,想到了是否可以通過頁面重啟Windows伺服器

到Google搜尋了一下,找到了一段似乎很普遍的代碼

事實證明,這段代碼在寫案頭應用例如Console或者Windows Form程式的時候可以正常運行,但是通過ASP.NET調用則無法通過

但是我還是把這段代碼貼出來,因為其中除了個別兩行外,其他的還是重啟伺服器的必須代碼

建立一個類,在裡面填入如下代碼:

首先是命名空間,調用Win API的時候,InteropServices不可少:
using System;
using System.Runtime.InteropServices;
然後是一系列的常量聲明: protected const int SE_PRIVILEGE_ENABLED = 0x2;
protected const int TOKEN_QUERY = 0x8;
protected const int TOKEN_ADJUST_PRIVILEGES = 0x20;
protected const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
protected const int EWX_LOGOFF = 0x0;
protected const int EWX_SHUTDOWN = 0x1;
protected const int EWX_REBOOT = 0x2;
protected const int EWX_FORCE = 0x4;
protected const int EWX_POWEROFF = 0x8;
protected const int EWX_FORCEIFHUNG = 0x10;
定義Luid結構,注意屬性: [StructLayout(LayoutKind.Sequential, Pack=1)]
protected struct LuidStruct {
    public int Count;
    public long Luid;
    public int Attr;
}
外部非託管DLL的聲明: [DllImport("kernel32.dll", ExactSpelling=true)]
protected static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll", SetLastError=true)]
protected static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

[DllImport("advapi32.dll", SetLastError=true)]
protected static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

[DllImport("advapi32.dll", SetLastError=true, ExactSpelling=true)]
protected static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref LuidStruct newst, int len, IntPtr prev, IntPtr relen);

[DllImport("user32.dll", SetLastError=true, ExactSpelling=true)]
protected static extern bool ExitWindowsEx(int flg, int rea);
在NT級的作業系統上,需要先通知Windows系統即將關機,並且要獲得關機的許可權

以下就是關機、重啟以及登出的實現: protected static void DoExitWindows(int flg) {
    LuidStruct tp;
    IntPtr hproc = GetCurrentProcess();
    IntPtr htok = IntPtr.Zero;

    OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
    tp.Count = 1;
    tp.Luid = 0;
    tp.Attr = SE_PRIVILEGE_ENABLED;
    LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
    AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
    ExitWindowsEx(flg, 0);
}

public static void Shutdown() {
    DoExitWindows(EWX_SHUTDOWN);
}

public static void Reboot() {
    DoExitWindows(EWX_REBOOT | EWX_FORCE);
}

public static void Logoff() {
    DoExitWindows(EWX_LOGOFF);
}
至此,重啟代碼結束,這段代碼可以很好地工作在互動環境下,也就是在使用者已經登入進Windows的情況下

但是ASP.NET是運行在非互動環境下的,查閱MSDN,在ExitWindowsEx函數定義下面發現這樣一段話:


The ExitWindowsEx function returns as soon as it has initiated the shutdown process. The shutdown or logoff then proceeds asynchronously. The function is designed to stop all processes in the caller's logon session. Therefore, if you are not the interactive user, the function can succeed without actually shutting down the computer. If you are not the interactive user, use the InitiateSystemShutdown or InitiateSystemShutdownEx function.

於是得到啟發,發現非互動下重啟伺服器不可以用ExitWindowsEx,需要將其替換成InitiateSystemShutdown:

[DllImport("advapi32.dll", SetLastError=true, ExactSpelling=false)]
protected static extern bool InitiateSystemShutdown(string name, string msg, int timeout, bool force, bool reboot);
參數解釋:
name:機器名,用於重啟區域網路內的其它機器,如果為 null 則是本機
msg:重啟訊息,會顯示在重啟訊息框上,在Windows 2003和XP中也會作為訊息日誌被儲存
timeout:如果不是0,那麼會顯示一個重新訊息框,倒計時timeout秒後重啟
force:強制重啟,不等待應用程式提示是否儲存工作,對於伺服器來說,應該是true
reboot:是否是重啟,如果是false,那麼做關機處理,對於伺服器,應該是true

先按照文章一開始的方法調用 AdjustTokenPrivileges 得到Privilege,然後在ASP.NET頁面裡面執行: InitiateSystemShutdown(null,null,0,true,true);
系統就重啟了

這裡有一點說下,如果重啟本機,那麼多半會返回Service Unavailable錯誤,這是因為在ASP.NET執行結束之前系統已經開始結束各個進程了,當然包括ASP.NET進程,算是正常表現,雖然看起來有些不太舒服

另外由於目前我只在自己機器上測試通過,所以沒有詳細研究許可權問題,所以無法確定在一般伺服器上是否可以正常運行

初步只想到可以用許可權類比解決,即在web.config檔案system.web節寫上<identity impersonate="true" userName="Administrator" password="pass">,不過沒有經過確認,有時間會嘗試一下。web.config不是很安全,所以這裡可能要藉助於DPAPI,有點扯遠了,就先到這裡吧。



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.