国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET模擬其他用戶進行關機

2024-07-10 13:09:08
字體:
來源:轉載
供稿:網友
菜鳥學堂:

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
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清苑县| 定兴县| 淄博市| 高淳县| 乌拉特前旗| 新巴尔虎左旗| 靖远县| 新河县| 湖口县| 仁寿县| 道孚县| 大姚县| 卢氏县| 新干县| 仲巴县| 岚皋县| 平度市| 贵德县| 当涂县| 革吉县| 客服| 莱州市| 什邡市| 民乐县| 忻州市| 武义县| 威远县| 常山县| 新巴尔虎左旗| 乌拉特前旗| 上林县| 南昌市| 万载县| 巴青县| 宝应县| 巩留县| 确山县| 本溪| 镇康县| 个旧市| 丹棱县|