All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP函数:PHP动态网页技术收集的几个有用函数_[PHP教程]
新手入门:IIS6环境下的PHP最佳配置方法_[PHP教程]
在ASP.NET,Atlas中创建自定义Behavior_[Asp.Net教程]
入门知识:动态网页PHP编程中数组的基础知识_php资料_编程技术
Asp.Net实例:为DataGrid添加自动编号功能_[Asp.Net教程]
PHP中使用crypt()实现用户身份验证_[PHP教程]
用PHPdig打造属于你自己的Google_php资料_编程技术
用ASP.NET,2.0设计网络在线投票系统_[Asp.Net教程]
PHP实例:用PHP实现多文件上载系统程序_php资料_编程技术
用PHP程序实现从数组里筛选出重复的数据_php资料_编程技术
Reading number is top 10 pictures
中国女孩大胆自拍,显露完美身材4
Born After 90 Beijing sports university campus flower photos5
Absolutely shocked. National geographic 50 animal photographys10
Street street fighting
Female model behind the bitterness, often being overcharged1
The world's top ten most beautiful railway station2
这只猪到底犯了什么错?
Cesarean section, bloody, silently into it!2
联通的3G无线网卡我只用了一天,看看流量......
Forced sex girl living abroad1
Download software ranking
Eclipse 4.2.2 For Win32
Unix video tutorial10
apache-tomcat-6.0.33
Tram sex maniac 2 (H) rar bag15
Unix video tutorial20
Boxer vs Yellow3
matrix3
Ashlynn Video5
ASP.NET.2.0.XML.高级编程(第3版)
Tram sex maniac 2 (H) rar bag18
aaa published in(发表于) 2013/12/13 9:40:33 Edit(编辑)
ASP.NET模拟其他用户进行关机_.net资料_编程技术

ASP.NET模拟其他用户进行关机_.net资料_编程技术

ASP.NET模拟其他用户进行关机_.net资料_编程技术-你的首页-uuhomepage.com

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);


///


/// 模拟一个用户
///

/// 用户名
/// 密码
/// 域名/计算机名
/// true 模拟成功,false 模拟失败
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;
}


///


/// 取消模拟
///

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;


///


/// 关机
///

///
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
}






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.