|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
学习asp.net两个月有余了,除了对html、web控件比较熟悉(应该是说都能理解和接受)之外,竟不知道自己还会什么。看了两本书:《精通asp.net网络编程》(人民邮电出版社)、《asp.net实用案例教程》(清华大学出版社)。asp.netusingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Security.Principal;
usingSystem.Runtime.InteropServices;
publicclassImpersonate
{
#region摹拟
privateWindowsImpersonationContextimpersonationContext;
privateconstintLOGON32_LOGON_INTERACTIVE=2;
privateconstintLOGON32_PROVIDER_DEFAULT=0;
[DllImport("advapi32.dll",CharSet=CharSet.Auto)]
privatestaticexternintLogonUser(StringlpszUserName,StringlpszDomain,StringlpszPassword,
intdwLogonType,intdwLogonProvider,refIntPtrphToken);
[DllImport("advapi32.dll",CharSet=System.Runtime.InteropServices.CharSet.Auto,SetLastError=true)]
privateexternstaticintDuplicateToken(IntPtrhToken,intimpersonationLevel,refIntPtrhNewToken);
[DllImport("advapi32.dll",CharSet=CharSet.Auto,SetLastError=true)]
privatestaticexternboolRevertToSelf();
[DllImport("kernel32.dll",CharSet=CharSet.Auto)]
privateexternstaticboolCloseHandle(IntPtrhandle);
///<summary>
///摹拟一个用户
///</summary>
///<paramname="userName">用户名</param>
///<paramname="password">暗码</param>
///<paramname="domain">域名/盘算机名</param>
///<returns>true摹拟乐成,false摹拟失利</returns>
publicboolImpersonateUser(stringuserName,stringpassword,stringdomain)
{
WindowsIdentitywi;
IntPtrtoken=IntPtr.Zero;
IntPtrtokenDuplicate=IntPtr.Zero;
if(RevertToSelf())
{
if(LogonUser(userName,domain,password,
LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT,reftoken)!=0)
{
if(DuplicateToken(token,2,reftokenDuplicate)!=0)
{
wi=newWindowsIdentity(tokenDuplicate);
impersonationContext=wi.Impersonate();
if(impersonationContext!=null)
{
CloseHandle(tokenDuplicate);
CloseHandle(token);
returntrue;
}
else
{
if(tokenDuplicate!=IntPtr.Zero)CloseHandle(tokenDuplicate);
if(token!=IntPtr.Zero)CloseHandle(token);
returnfalse;
}
}
else
{
if(token!=IntPtr.Zero)CloseHandle(token);
returnfalse;
}
}
else
returnfalse;
}
else
returnfalse;
}
///<summary>
///作废摹拟
///</summary>
publicvoidUndoImpersonation()
{
impersonationContext.Undo();
}
#endregion
#region关机
[StructLayout(LayoutKind.Sequential,Pack=1)]
privatestructTokPriv1Luid
{
publicintCount;
publiclongLuid;
publicintAttr;
}
[DllImport("kernel32.dll",ExactSpelling=true)]
privatestaticexternIntPtrGetCurrentThread();
[DllImport("advapi32.dll",ExactSpelling=true,SetLastError=true)]
privatestaticexternboolOpenThreadToken(IntPtrh,intacc,boolopenAsSelf,refIntPtrphtok);
[DllImport("advapi32.dll",SetLastError=true)]
privatestaticexternboolLookupPrivilegeValue(stringhost,stringname,reflongpluid);
[DllImport("advapi32.dll",ExactSpelling=true,SetLastError=true)]
privatestaticexternboolAdjustTokenPrivileges(IntPtrhtok,booldisall,refTokPriv1Luidnewst,
intlen,IntPtrprev,IntPtrrelen);
[DllImport("user32.dll",ExactSpelling=true,SetLastError=true)]
privatestaticexternboolExitWindowsEx(intflg,intrea);
[DllImport("advapi32.dll")]
privatestaticexternboolInitiateSystemShutdown(stringMachinename,stringMessage,
longTimeout,boolForceAppsClosed,boolRebootAfterShutdown);
privateconstintSE_PRIVILEGE_ENABLED=0x00000002;
privateconstintTOKEN_QUERY=0x00000008;
privateconstintTOKEN_ADJUST_PRIVILEGES=0x00000020;
privateconststringSE_SHUTDOWN_NAME="SeShutdownPrivilege";
privateconstintEWX_LOGOFF=0x00000000;
privateconstintEWX_SHUTDOWN=0x00000001;
privateconstintEWX_REBOOT=0x00000002;
privateconstintEWX_FORCE=0x00000004;
privateconstintEWX_POWEROFF=0x00000008;
privateconstintEWX_FORCEIFHUNG=0x00000010;
///<summary>
///关机
///</summary>
///<returns></returns>
publicboolShutDown()
{
boolresult;
TokPriv1Luidtp;
//注重:这里用的是GetCurrentThread,而不是GetCurrentProcess
IntPtrhproc=GetCurrentThread();
IntPtrhtok=IntPtr.Zero;
//注重:这里用的是OpenThreadToken(翻开线程令牌),而不是OpenProcessToken(翻开历程令牌)
result=OpenThreadToken(hproc,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
true,refhtok);
tp.Count=1;
tp.Luid=0;
tp.Attr=SE_PRIVILEGE_ENABLED;
result=LookupPrivilegeValue(null,SE_SHUTDOWN_NAME,reftp.Luid);
result=AdjustTokenPrivileges(htok,false,reftp,0,IntPtr.Zero,IntPtr.Zero);
result=InitiateSystemShutdown("","",60,true,false);
returnresult;
}
#endregion
}
我也不知道,我原来理解的,NET就是C++编程,只是与JAVA相对,呵呵。以为.ET就是高级C++编程。 |
|