menggunakan Sistem;
menggunakan System.Collections.Generik;
menggunakan Sistem.Teks;
menggunakan Sistem.Keamanan.Principal;
menggunakan System.Runtime.InteropServices;
kelas publik Meniru
{
#wilayah 模拟
pribadi WindowsImpersonationContext peniruanContext;
konstanta pribadi int LOGON32_LOGON_INTERACTIVE = 2;
konstanta pribadi 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 DuplikatToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
bool eksternal statis pribadi RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
bool statis eksternal pribadi CloseHandle(pegangan IntPtr);
/// <ringkasan>
/// 模拟一个用户
/// </ringkasan>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <param name="domain">域名/计算机名</param>
/// <returns>benar 模拟成功,salah 模拟失败</returns>
public bool ImpersonateUser (string nama pengguna, kata sandi string, domain string)
{
WindowsIdentitas dengan;
Token IntPtr = IntPtr.Zero;
IntPtr tokenDuplikat = IntPtr.Zero;
jika (Kembalikan KeSelf())
{
if (LogonUser(Nama pengguna, domain, kata sandi,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token referensi) != 0)
{
jika (DuplikatToken(token, 2, ref tokenDuplikat) != 0)
{
wi = WindowsIdentity baru (tokenDuplikat);
peniruanContext = wi.Impersonate();
jika (impersonationContext != null)
{
CloseHandle(tokenDuplikat);
CloseHandle(token);
kembali benar;
}
kalau tidak
{
if (tokenDuplikat != IntPtr.Zero) CloseHandle(tokenDuplikat);
jika (token!= IntPtr.Zero) CloseHandle(token);
kembali salah;
}
}
kalau tidak
{
jika (token!= IntPtr.Zero) CloseHandle(token);
kembali salah;
}
}
kalau tidak
kembali salah;
}
kalau tidak
kembali salah;
}
/// <ringkasan>
/// 取消模拟
/// </ringkasan>
kekosongan publik Urungkan Peniruan Identitas()
{
peniruanContext.Undo();
}
#endregion
#region 关机
[StructLayout(LayoutKind.Sequential, Paket = 1)]
struct pribadi TokPriv1Luid
{
Hitungan int publik;
Luid panjang publik;
publik int Attr;
}
[DllImport("kernel32.dll", ExactSpelling = benar)]
swasta statis eksternal IntPtr GetCurrentThread();
[DllImport("advapi32.dll", ExactSpelling = benar, SetLastError = benar)]
private static extern bool OpenThreadToken(IntPtr h, int acc, bool openAsSelf, ref IntPtr phtok);
[DllImport("advapi32.dll", SetLastError = true)]
bool eksternal statis pribadi LookupPrivilegeValue (host string, nama string, ref pluid panjang);
[DllImport("advapi32.dll", ExactSpelling = benar, SetLastError = benar)]
private static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid terbaru,
int len, IntPtr sebelumnya, IntPtr relen);
[DllImport("user32.dll", ExactSpelling = benar, SetLastError = benar)]
bool eksternal statis pribadi ExitWindowsEx(int flg, int rea);
[Impor Dll("advapi32.dll")]
private static extern bool InitiateSystemShutdown(string Nama Mesin, string Pesan,
Timeout yang lama, bool ForceAppsClosed, bool RebootAfterShutdown);
pribadi const int SE_PRIVILEGE_ENABLED = 0x00000002;
pribadi const int TOKEN_QUERY = 0x00000008;
pribadi const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
string const pribadi SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
konstanta pribadi int EWX_LOGOFF = 0x00000000;
pribadi const int EWX_SHUTDOWN = 0x00000001;
konstanta pribadi int EWX_REBOOT = 0x00000002;
konstanta pribadi int EWX_FORCE = 0x00000004;
konstanta pribadi int EWX_POWEROFF = 0x00000008;
konstanta pribadi int EWX_FORCEIFHUNG = 0x00000010;
/// <ringkasan>
/// 关机
/// </ringkasan>
/// <pengembalian></pengembalian>
bool publik ShutDown()
{
hasil yang buruk;
TokPriv1Luid tp;
//注意:这里用的是GetCurrentThread,而不是GetCurrentProcess
IntPtr hproc = GetCurrentThread();
IntPtr htok = IntPtr.Zero;
//注意:这里用的是OpenThreadToken(打开线程令牌),而不是OpenProcessToken(打开进程令牌)
hasil = OpenThreadToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
benar, ref htok);
tp.Hitungan = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
hasil = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
hasil = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
hasil = InitiateSystemShutdown("", "", 60, benar, salah);
hasil pengembalian;
}
#wilayah akhir
}
http://www.cnblogs.com/anjou/archive/2006/11/30/577279.html