This paper gives the source program of using ASP.net to simulate the shutdown of other users.
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Security.Principal;
Using System.Runtime.InteropServices;
public class impersonate
{
#region Simulation
Private WindowsImpersonationContext impersonationcontext;
Private Const int logon32_logon_interactive = 2;
Private Const int logon32_provider_default = 0;
[DllImport ("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int LogonUser (String lpszUserName,
String lpszdomain, String Lpszpassword,
int dwLogonType, int dwlogonprovider, ref IntPtr phtoken);
[DllImport ("advapi32.dll", CharSet =
System.Runtime.InteropServices.CharSet.Auto, SetLastError = True)]
private extern static int DuplicateToken (INTPTR htoken,
int impersonationlevel, ref IntPtr hNewToken);
[DllImport ("advapi32.dll", CharSet = CharSet.Auto, SetLastError = True)]
private static extern bool RevertToSelf ();
[DllImport ("kernel32.dll", CharSet = CharSet.Auto)]
private extern static bool CloseHandle (INTPTR handle);
<summary>
Simulate a user
</summary>
<param name= "UserName" > Username </param>
<param name= "password" > Password </param>
<param name= "domain" > Domain/Computer name </param>
<returns>true simulation succeeded, false impersonation failed </returns>
public bool ImpersonateUser (string userName, string password, string domain)
{
WindowsIdentity WI;
IntPtr token = IntPtr.Zero;
IntPtr tokenduplicate = IntPtr.Zero;
if (RevertToSelf ())
{
if (LogonUser (userName, domain, password,
Logon32_logon_interactive, Logon32_provider_default, ref token)!= 0)
{
if (DuplicateToken (token, 2, ref tokenduplicate)!= 0)
{
WI = new WindowsIdentity (tokenduplicate);
impersonationcontext = wi. Impersonate ();
if (impersonationcontext!= null)
{
CloseHandle (tokenduplicate);
CloseHandle (token);
return true;
}
Else
{
if (tokenduplicate!= IntPtr.Zero)
CloseHandle (tokenduplicate);
if (token!= intptr.zero) CloseHandle (token);
return false;
}
}
Else
{
if (token!= intptr.zero) CloseHandle (token);
return false;
}
}
Else
return false;
}
Else
return false;
}
<summary>
Canceling impersonation
</summary>
public void undoimpersonation ()
{
Impersonationcontext.undo ();
}
#endregion
#region shutdown
[StructLayout (layoutkind.sequential, Pack = 1)]
private struct TOKPRIV1LUID
{
public int Count;
public long Luid;
public int Attr;
}
[DllImport ("kernel32.dll", ExactSpelling = True)]
private static extern IntPtr GetCurrentThread ();
[DllImport ("advapi32.dll", ExactSpelling = True, SetLastError = True)]
private static extern bool OpenThreadToken (IntPtr H,
int ACC, bool openasself, ref INTPTR Phtok);
[DllImport ("advapi32.dll", SetLastError = True)]
private static extern bool Lookupprivilegevalue (string host, string name,
Ref long Pluid);
[DllImport ("advapi32.dll", ExactSpelling = True, SetLastError = True)]
private static extern bool AdjustTokenPrivileges (INTPTR Htok, bool Disall,
Ref Tokpriv1luid Newst,
int Len, IntPtr prev, IntPtr Relen);
[DllImport ("user32.dll", ExactSpelling = True, SetLastError = True)]
private static extern bool ExitWindowsEx (int flg, int rea);
[DllImport ("advapi32.dll")]
private static extern bool InitiateSystemShutdown (string machinename,
String message,
Long Timeout, BOOL forceappsclosed, bool Rebootaftershutdown);
Private Const int se_privilege_enabled = 0x00000002;
Private Const int token_query = 0x00000008;
Private Const int token_adjust_privileges = 0x00000020;
Private Const string se_shutdown_name = "SeShutdownPrivilege";
Private Const int ewx_logoff = 0x00000000;
Private Const int ewx_shutdown = 0x00000001;
Private Const int ewx_reboot = 0x00000002;
Private Const int ewx_force = 0x00000004;
Private Const int ewx_poweroff = 0x00000008;
Private Const int Ewx_forceifhung = 0x00000010;
<summary>
Shutdown
</summary>
<returns></returns>
public bool ShutDown ()
{
BOOL result;
TOKPRIV1LUID TP;
Note: This is getcurrentthread, not getcurrentprocess.
IntPtr hproc = GetCurrentThread ();
IntPtr Htok = IntPtr.Zero;
Note: This is OpenThreadToken (open line begins thread card), not OpenProcessToken (open process token)
result = OpenThreadToken (hproc, Token_adjust_privileges | Token_query,
True, ref Htok);
Tp. Count = 1;
Tp. LUID = 0;
Tp. Attr = se_privilege_enabled;
result = Lookupprivilegevalue (null, SE_SHUTDOWN_NAME, ref TP. LUID);
result = AdjustTokenPrivileges (Htok, FALSE, ref TP, 0, IntPtr.Zero,
IntPtr.Zero);
result = InitiateSystemShutdown ("", "", A, true, false);
return result;
}
#endregion
}