使用系统;
使用 System.Collections.Generic;
使用系统文本;
使用 System.Security.Principal;
使用 System.Runtime.InteropServices;
公开课模仿
{
#region 模拟
私有 WindowsImpersonationContext 模拟上下文;
私有常量 int LOGON32_LOGON_INTERACTIVE = 2;
私有常量 int LOGON32_PROVIDER_DEFAULT = 0;
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
私有静态 extern int LogonUser(字符串 lpszUserName, 字符串 lpszDomain, 字符串 lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport(“advapi32.dll”,CharSet = System.Runtime.InteropServices.CharSet.Auto,SetLastError = true)]
私有 extern static int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
私有静态 extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
私有 extern static bool CloseHandle(IntPtr 句柄);
/// <摘要>
/// 模拟一个用户
/// 摘要>
/// 用户名
/// 密码
/// 域名/计算机名
///
public bool ImpersonateUser(字符串用户名,字符串密码,字符串域)
{
WindowsIdentity 无线;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUser(用户名, 域, 密码,
LOGON32_LOGON_INTERACTIVE、LOGON32_PROVIDER_DEFAULT、参考令牌)!= 0)
{
if (DuplicateToken(令牌, 2, 参考 tokenDuplicate) != 0)
{
wi = new WindowsIdentity(tokenDuplicate);
impersonationContext = wi.Impersonate();
if (impersonationContext != null)
{
关闭句柄(tokenDuplicate);
关闭句柄(令牌);
返回真;
}
别的
{
if (tokenDuplicate != IntPtr.Zero) CloseHandle(tokenDuplicate);
if (token!= IntPtr.Zero) CloseHandle(token);
返回假;
}
}
别的
{
if (token!= IntPtr.Zero) CloseHandle(token);
返回假;
}
}
别的
返回假;
}
别的
返回假;
}
/// <摘要>
/// 取消模拟
/// 摘要>
公共无效撤销模拟()
{
impersonationContext.Undo();
}
#endregion
#region 关闭
[StructLayout(LayoutKind.Sequential, Pack = 1)]
私有结构 TokPriv1Luid
{
公共 int 计数;
公众长Luid;
公共 int 属性;
[DllImport("kernel32.dll", ExactSpelling = true)
]
私有静态 extern IntPtr GetCurrentThread();
[DllImport(“advapi32.dll”,ExactSpelling = true,SetLastError = true)]
私有静态 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)]
私人静态外部布尔AdjustTokenPrivileges(IntPtr htok,布尔disall,ref TokPriv1Luid newst,
int len、IntPtr prev、IntPtr relen);
[DllImport(“user32.dll”,ExactSpelling = true,SetLastError = true)]
私有静态 extern bool ExitWindowsEx(int flg, int rea);
[DllImport("advapi32.dll")]
私有静态 extern bool InitiateSystemShutdown(string Machinename, string Message,
长超时,布尔 ForceAppsClosed,布尔 RebootAfterShutdown);
私有常量 int SE_PRIVILEGE_ENABLED = 0x00000002;
私有常量 int TOKEN_QUERY = 0x00000008;
私有常量 int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
私有 const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
私有常量 int EWX_LOGOFF = 0x00000000;
私有常量int EWX_SHUTDOWN = 0x00000001;
私有常量int EWX_REBOOT = 0x00000002;
私有常量int EWX_FORCE = 0x00000004;
私有常量int EWX_POWEROFF = 0x00000008;
私有常量 int EWX_FORCEIFHUNG = 0x00000010;
/// <摘要>
/// 关机
/// 摘要>
/// <返回>返回>
公共布尔关闭()
{
布尔结果;
TokPriv1Luid tp;
//注意:这里用的是GetCurrentThread,而不是GetCurrentProcess
IntPtr hproc = GetCurrentThread();
IntPtr htok = IntPtr.Zero;
//注意:这里用的是OpenThreadToken(打开线程令牌),而不是OpenProcessToken(打开进程令牌)
结果 = OpenThreadToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
正确,参考 htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
结果 = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
结果=AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
结果 = InitiateSystemShutdown("", "", 60, true, false);
返回结果;
}
#endregion
}
http://www.cnblogs.com/anjou/archive/2006/11/30/577279.html