How to shut down a machine from ASP.NET

I have a machine running Windows Home Server v1 (WHS is based on Windows Server 2003 and runs on IIS6), and here I have a working webservice that I create in C # VS2008 (dotnet 3.5). From within the web service, I want to be able to; 1 Make sure certain Windows services are running; 2 Starting some Windows services 3 Stop some Windows services 4 Restart the computer 5 Shutting down the machine

For 1-3, I use impersonation to elevate an ASPNET user to a local administrator (this is just me running it on a local secure network), and then "ServiceController" to manage the services. It works great.

For 4 and 5, I am having problems and cannot make it work.

If I use System.Diagnostics.Process to invoke the shutdown.exe command with the / s / f parameters, the process runs without any errors, but does nothing! No shutdown, no exception, nothing, and I can not understand why. I tried to set the local administrator username and password, but this does not help, and the outstanding user call does not help.

My code

string shut_args = "/s /f /t 10"; Process process1 = new Process(); process1.StartInfo.FileName = "shutdown.exe"; process1.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System); SecureString password = new SecureString(); foreach (char c in "mypassword") password.AppendChar(c); process1.StartInfo.Password = password; process1.StartInfo.Domain = "homeserver"; process1.StartInfo.UserName = "Administrator"; process1.StartInfo.Arguments = shut_args; process1.StartInfo.CreateNoWindow = true; process1.StartInfo.UseShellExecute = false; process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; impersonateValidUser(); process1.Start(); 

So instead I tried to use WMI (code taken from another post here), but here I get the error "Privilege not held" when I try to call InvokeMethod

Mycode

 ManagementBaseObject mboShutdown = null; ManagementClass mcWin32 = new ManagementClass ("Win32_OperatingSystem"); mcWin32.Get(); mcWin32.Scope.Options.Impersonation = ImpersonationLevel.Impersonate; mcWin32.Scope.Options.Authentication = AuthenticationLevel.Connect; mcWin32.Scope.Options.EnablePrivileges = true; ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown"); // Flag 1 means we want to shut down the system. Use "2" to reboot. mboShutdownParams["Flags"] = "1"; mboShutdownParams["Reserved"] = "0"; foreach (ManagementObject manObj in mcWin32.GetInstances()) { mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null); } 

I also used all of my security settings for the ASPNET and NETWORK_SERVICE user, and they have rights to shut down the server, and WMI security settings are also set for these users. But I just can’t understand what’s wrong.

Any ideas?

Greetings

+4
source share
2 answers

Have you tried running a simple batch file instead of starting the shutdown process in ASP.NET? The whole process is described here: remote shutdown / restart using ASP.NET

+3
source
An old question, but I wanted to do it myself and just understood the answer. So try the following:
 Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "shutdown"; startInfo.Arguments = "/s /t 5"; startInfo.UseShellExecute = true; startInfo.Verb = "runas"; process.StartInfo = startInfo; process.Start(); 
0
source

All Articles