首 页 | 精品电影 | 音乐天堂 | 在线游戏 | Flash MTV | 三湘书屋 | 幽默笑话 | 三湘图库 | 美女写真 | IT知识库 | QQ贴图 | 加入书签

网页制作网络编程图形图象操作系统冲浪宝典软件教学网络安全认证考试通信技术电子商务业内动态书籍教程原码

最近更新 文章分类 多媒体类 精品软件

本站搜索:
您的位置:三湘时空 -> IT知识库 -> 文章分类 -> ASP.NET技巧 -> ASP.NET模拟其他用户进行关机
ASP.NET模拟其他用户进行关机


文章类别:ASP.NET技巧 来源: 作者: 发表日期:2006-12-7 字体:[ ]

小游戏 | 在线影院 | 幽默笑话 | 源码下载 | Flash MTV | 音乐试听 | 书屋 | 美女写真

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

上一篇:得到随机字符串算法 下一篇:用C#动态创建Access数据库
本栏目热门文章
·如何实现无刷新的DropdownList联动效果 2005-10-4
·使用HttpWebRequest向网站模拟上传数据 2005-10-4
·ASP.NET中文件上传下载方法集合 2006-5-28
·分享个极好的无刷新二级联动下拉列表,同样适用与firefox 2005-10-19
·ASP.NET2.0实现无刷新客户端回调 2005-11-13
·当DataSet中包含主/子表时,Update更新步骤 2005-10-6
·在Web DataGrid中当鼠标移到某行与离开时行的颜色发生改变( 2005-10-4
·ASP.NET中实现Flash与.NET的紧密集成 2005-11-21
·关于Asp.net页面Page_Load被执行两次的问题 2005-10-4
·用window.location.href实现刷新另个框架页面 2006-6-20
新近更新文章
·ASP.NET模拟其他用户进行关机 2006-12-7
·得到随机字符串算法 2006-12-7
·最简单的asp.net分页 2006-12-7
·Scott Mitchell 的ASP.NET 2.0数据教程之应用 2006-12-7
·深入聊聊Array的sort方法的使用技巧.详细点评protype. 2006-12-7
·用完HttpWebResponse时别忘了调用Close方法 2006-12-7
·SQL Server精简版支不支持ASP.NET? 2006-12-7
·对NDoc支持.net2.0的异常分析及解决 2006-12-7
·比较:HyperLink控件、LinkButton控件 之间的异同 2006-12-7
·ASP.NET 2.0高级数据处理之冲突检测 2006-12-7
首 页 | 软件发布 | 广告联系 | 下载帮助 | 意见反馈 | 网站地图
  CopyRight? 2002-2004 WWW.SXSKY.NET? All Rights Reserved
三湘时空 站长QQ:82675303 Email: