usando o sistema;
usando System.Collections.Generic;
usando System.Text;
usando System.Security.Principal;
usando System.Runtime.InteropServices;
classe pública personificar
{
#região
private WindowsImpersonationContext personificaçãoContext;
privado const int LOGON32_LOGON_INTERACTIVE = 2;
privado 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 (identificador IntPtr);
/// <resumo>
/// 模拟一个用户
/// </sumário>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <param name="domain">域名/计算机名</param>
/// <returns>true 模拟成功,false 模拟失败</returns>
public bool ImpersonateUser(string nome_do_usuário, string senha, string domínio)
{
WindowsIdentidade wi;
Token IntPtr = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUser(nomedeusuário, domínio, senha,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token de referência) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
wi = new WindowsIdentity(tokenDuplicate);
personificaçãoContext = wi.Impersonate();
if (impersonationContext! = Nulo)
{
CloseHandle(tokenDuplicado);
CloseHandle(token);
retornar verdadeiro;
}
outro
{
if (tokenDuplicate! = IntPtr.Zero) CloseHandle(tokenDuplicate);
if (token! = IntPtr.Zero) CloseHandle(token);
retornar falso;
}
}
outro
{
if (token! = IntPtr.Zero) CloseHandle(token);
retornar falso;
}
}
outro
retornar falso;
}
outro
retornar falso;
}
/// <resumo>
/// 取消模拟
/// </sumário>
public void UndoImpersonation()
{
personificaçãoContext.Undo();
}
#endregion
#region
[StructLayout(LayoutKind.Sequential, Pacote = 1)]
estrutura privada TokPriv1Luid
{
contagem interna pública;
público longo Luid;
público int Atributo;
}
[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 anterior, 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 Nome da máquina, string Mensagem,
longo tempo limite, bool ForceAppsClosed, bool RebootAfterShutdown);
private const int SE_PRIVILEGE_ENABLED = 0x00000002;
privado const int TOKEN_QUERY = 0x00000008;
private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
string const privada SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
privado const int EWX_LOGOFF = 0x00000000;
private const int EWX_SHUTDOWN = 0x00000001;
privado const int EWX_REBOOT = 0x00000002;
privado const int EWX_FORCE = 0x00000004;
privado const int EWX_POWEROFF = 0x00000008;
privado const int EWX_FORCEIFHUNG = 0x00000010;
/// <resumo>
/// 关机
/// </sumário>
/// <retorna></retorna>
bool público Desligar()
{
resultado booleano;
TokPriv1Luid tp;
//注意:这里用的是GetCurrentThread,而不是GetCurrentProcess
IntPtr hproc = GetCurrentThread();
IntPtr htok = IntPtr.Zero;
//注意:这里用的是OpenThreadToken(打开线程令牌),而不是OpenProcessToken(打开进程令牌)
resultado = OpenThreadToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
verdadeiro, ref htok);
tp.Contagem = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
resultado = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
resultado = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
resultado = InitiateSystemShutdown("", "", 60, verdadeiro, falso);
resultado de retorno;
}
#endregion
}
http://www.cnblogs.com/anjou/archive/2006/11/30/577279.html