当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  ASP.NET模拟其他用户进行关机

 ASP.NET模拟其他用户进行关机

点击次数:23 次 发布日期:2008-11-26 11:52:51 作者:源代码网
源代码网推荐      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
源代码网推荐  }
源代码网推荐  http://www.cnblogs.com/anjou/archive/2006/11/30/577279.html
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华