Went to Google and found a piece of code that seemed very common.
It turns out that this code works well when it comes to writing desktop applications such as console or Windows Form programs, but it doesn't work through the asp.net call.
But I'm going to post this code, because in addition to the individual two lines, the other is the necessary code to reboot the server
Create a new class that fills in the following code:
First is the namespace, when the win API is invoked, the interopservices is not less:
The following are the referenced contents: Using System; using System.Runtime.InteropServices; then a series of constant declarations: 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; Define the LUID structure, note the properties: [StructLayout (LayoutKind.Sequential, pack=1)] protected struct Luidstruct { public int Count; public long luid; public int Attr; } Declaration of an external unmanaged 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, I Ntptr Relen); [DllImport ("user32.dll", Setlasterror=true, Exactspelling=true)] protected static extern bool ExitWindowsEx (int flg, int rea); On an NT-level operating system, you need to notify Windows that the system is about to shut down and get permissions to shutdown The following is the implementation of shutdown, reboot, and logoff: 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); } |
Now that the restart code is over, this code works well in the interactive environment, where the user is already logged into Windows
But ASP.net is run in a non-interactive environment, look up MSDN, and find such a passage under the ExitWindowsEx function definition:
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 are not the interactive user, use the InitiateSystemShutdown or InitiateSystemShutdownEx function.
It was then inspired to discover that the ExitWindowsEx server could not be used without interactivity, and that it needed to be replaced with InitiateSystemShutdown:
[DllImport ("advapi32.dll", Setlasterror=true, Exactspelling=false)]
protected static extern bool InitiateSystemShutdown (string name, string msg, int timeout, BOOL force, BOOL reboot);
Parameter explanation:
Name: machine name used to reboot other machines within the LAN, if NULL is native
Msg: The restart message appears on the Restart message box and is also saved as a message log in Windows 2003 and XP
Timeout: If not 0, then a new message box will be displayed with the countdown timeout seconds to reboot
Force: Force reboot, do not wait for application prompts to save work, should be true for server
Reboot: is reboot, if False, then do shutdown processing, for the server, should be true
First in accordance with the method of the beginning of the article call AdjustTokenPrivileges get privilege, and then in the asp.net page execution: InitiateSystemShutdown (null,null,0,true,true);
The system is restarted.
The point here is that if you reboot this machine, you will probably return the service unavailable error, because the system has already started to end the process before the ASP.net execution is complete, including the ASP.net process, which is normal, although it seems a little uncomfortable.
In addition, because I am currently only testing on my own machine pass, so did not study the permissions issue in detail, so I can not determine whether the normal server to run
Initially only thought can use the permission simulation solves, namely in Web.config file System.Web section writes <identity impersonate= "true" Username= "the Administrator" password= "pass ", but not confirmed, have time to try." Web.config is not very safe, so here may have to rely on the DPAPI, a bit far away, the first here.