using system;
using system.collections.generic;
using system.text;
using system.security.principal;
using system.runtime.interopservices;
public class impersonate
{
#region 模拟
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>
/// 模拟一个用户
/// </summary>
/// <param name="username">用户名</param>
/// <param name="password">密码</param>
/// <param name="domain">域名/计算机名</param>
/// <returns>true 模拟成功,false 模拟失败</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>
/// 取消模拟
/// </summary>
public void undoimpersonation()
{
impersonationcontext.undo();
}
#endregion
#region 关机
[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>
/// 关机
/// </summary>
/// <returns></returns>
public bool shutdown()
{
bool result;
tokpriv1luid tp;
//注意:这里用的是getcurrentthread,而不是getcurrentprocess
intptr hproc = getcurrentthread();
intptr htok = intptr.zero;
//注意:这里用的是openthreadtoken(打开线程令牌),而不是openprocesstoken(打开进程令牌)
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
}
新闻热点
疑难解答
图片精选