Programatically Shutdown of a Computer without Administrative Privileges

Posted by Al on Sat, January 30 at 13:42:53 in C#

I recently fixed a bug at work where one of our applications could not shutdown the computer it was running on when the user logged out.  It turned out the problem was introduced when we recently modified the software to run under a non-administrator account.  We had made the system more secure, but the program no longer had sufficient privileges to shutdown the computer.  The fix for this was to have the software invoke the shutdown program using an administrator account.  Since the solution might be useful to someone else, I’m posting the code that I implemented.

Code appears after the break…

public void ShutdownSystem()
{
   
// SecureString encrypts the contents in memory to prevent unauthorized access
   
System.Security.SecureString password = new System.Security.SecureString();

   
// Create the password a character at a time
   
password.AppendChar('M');
   
password.AppendChar('y');
   
password.AppendChar('@');
   
password.AppendChar('P');
   
password.AppendChar('a');
   
password.AppendChar('s');
   
password.AppendChar('s');
   
password.AppendChar('w');
   
password.AppendChar('o');
   
password.AppendChar('r');
   
password.AppendChar('d');
   
password.MakeReadOnly();

   
// Invoke the shutdown process, using administrator privileges
   
System.Diagnostics.Process.Start(@"c:\windows\system32\shutdown.exe"
      
"-s -t 0 -f""Administrator"password"MyDomain");

Posted by Al on 01/30/10 at 01:42 PM in C#

Leave a Comment

Name:

Email:

Location:

URL:

Notify me of follow-up comments?

Submit the word you see below:


Comments