王朝网络
分享
 
 
 

通过ASP.NET页面重启服务器

王朝asp·作者佚名  2008-06-09
宽屏版  字体: |||超大  

最近在设计网站后台管理系统的时候,想到了是否可以通过页面重启Windows服务器

到Google搜索了一下,找到了一段似乎很普遍的代码

事实证明,这段代码在写桌面应用例如Console或者Windows Form程序的时候可以正常运行,但是通过ASP.NET调用则无法通过

但是我还是把这段代码贴出来,因为其中除了个别两行外,其他的还是重启服务器的必须代码

新建一个类,在里面填入如下代码:

首先是命名空间,调用Win API的时候,InteropServices不可少:

using System;

using System.Runtime.InteropServices;

然后是一系列的常量声明: protected const int SE_PRIVILEGE_ENABLED = 0x2;

protected const int TOKEN_QUERY = 0x8;

protected const int TOKEN_ADJUST_PRIVILEGES = 0x20;

protected const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

protected const int EWX_LOGOFF = 0x0;

protected const int EWX_SHUTDOWN = 0x1;

protected const int EWX_REBOOT = 0x2;

protected const int EWX_FORCE = 0x4;

protected const int EWX_POWEROFF = 0x8;

protected const int EWX_FORCEIFHUNG = 0x10;

定义Luid结构,注意属性: [StructLayout(LayoutKind.Sequential, Pack=1)]

protected struct LuidStruct {

public int Count;

public long Luid;

public int Attr;

}

外部非托管DLL的声明: [DllImport("kernel32.dll", ExactSpelling=true)]

protected static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll", SetLastError=true)]

protected static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

[DllImport("advapi32.dll", SetLastError=true)]

protected static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

[DllImport("advapi32.dll", SetLastError=true, ExactSpelling=true)]

protected static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref LuidStruct newst, int len, IntPtr prev, IntPtr relen);

[DllImport("user32.dll", SetLastError=true, ExactSpelling=true)]

protected static extern bool ExitWindowsEx(int flg, int rea);

在NT级的操作系统上,需要先通知Windows系统即将关机,并且要获得关机的权限

以下就是关机、重启以及注销的实现: protected static void DoExitWindows(int flg) {

LuidStruct tp;

IntPtr hproc = GetCurrentProcess();

IntPtr htok = IntPtr.Zero;

OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);

tp.Count = 1;

tp.Luid = 0;

tp.Attr = SE_PRIVILEGE_ENABLED;

LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);

AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);

ExitWindowsEx(flg, 0);

}

public static void Shutdown() {

DoExitWindows(EWX_SHUTDOWN);

}

public static void Reboot() {

DoExitWindows(EWX_REBOOT | EWX_FORCE);

}

public static void Logoff() {

DoExitWindows(EWX_LOGOFF);

}

至此,重启代码结束,这段代码可以很好地工作在交互环境下,也就是在用户已经登录进Windows的情况下

但是ASP.NET是运行在非交互环境下的,查阅MSDN,在ExitWindowsEx函数定义下面发现这样一段话:

The ExitWindowsEx function returns as soon as it has initiated the shutdown process. The shutdown or logoff then proceeds asynchronously. The function is designed to stop all processes in the caller's logon session. Therefore, if you are not the interactive user, the function can succeed without actually shutting down the computer. If you are not the interactive user, use the InitiateSystemShutdown or InitiateSystemShutdownEx function.

于是得到启发,发现非交互下重启服务器不可以用ExitWindowsEx,需要将其替换成InitiateSystemShutdown:

[DllImport("advapi32.dll", SetLastError=true, ExactSpelling=false)]

protected static extern bool InitiateSystemShutdown(string name, string msg, int timeout, bool force, bool reboot);

参数解释:

name:机器名,用于重启局域网内的其它机器,如果为 null 则是本机

msg:重启消息,会显示在重启消息框上,在Windows 2003和XP中也会作为消息日志被保存

timeout:如果不是0,那么会显示一个重新消息框,倒计时timeout秒后重启

force:强制重启,不等待应用程序提示是否保存工作,对于服务器来说,应该是true

reboot:是否是重启,如果是false,那么做关机处理,对于服务器,应该是true

先按照文章一开始的方法调用 AdjustTokenPrivileges 得到Privilege,然后在ASP.NET页面里面执行: InitiateSystemShutdown(null,null,0,true,true);

系统就重启了

这里有一点说下,如果重启本机,那么多半会返回Service Unavailable错误,这是因为在ASP.NET执行结束之前系统已经开始结束各个进程了,当然包括ASP.NET进程,算是正常表现,虽然看起来有些不太舒服

另外由于目前我只在自己机器上测试通过,所以没有详细研究权限问题,所以无法确定在一般服务器上是否可以正常运行

初步只想到可以用权限模拟解决,即在web.config文件system.web节写上<identity impersonate="true" userName="Administrator" password="pass">,不过没有经过确认,有时间会尝试一下。web.config不是很安全,所以这里可能要借助于DPAPI,有点扯远了,就先到这里吧。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有