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"> User name </param>
/// <Param name = "password"> password </param>
/// <Param name = "domain"> domain name/computer name </param>
/// <Returns> true: Simulation succeeded, false: Simulation 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>
/// Cancel the simulation
/// </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: GetCurrentThread is used instead of GetCurrentProcess.
IntPtr hproc = GetCurrentThread ();
IntPtr htok = IntPtr. Zero;
// Note: OpenThreadToken is used here (to open the thread token), instead of OpenProcessToken (to open the 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 ("", "", 60, true, false );
Return result;
}
# Endregion
}