62,074
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" %>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System.Web.Security" %>
<%@ Import Namespace = "System.Security.Principal" %>
<%@ Import Namespace = "System.Runtime.InteropServices" %>
<%@ Import Namespace = "System.IO" %>
<%@ Import Namespace = "System.Diagnostics" %>
<SCRIPT runat="server">
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public 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)]
public extern static int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);
public void Page_Load(Object s, EventArgs e)
{
}
private bool impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); impersonationContext = tempWindowsIdentity.Impersonate(); if (impersonationContext != null) return true; else return false;
}
else return false;
}
else return false;
}
private void undoImpersonation()
{
impersonationContext.Undo();
}
public string ExeCommand(string commandText)
{
if (impersonateValidUser("Administrator", "Administrators", "xin"))
{
Process p = new Process();
p.StartInfo.FileName = @"C:\WINDOWS\system32\cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
//try
//{
p.Start();
p.StandardInput.WriteLine(commandText);
p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
//}
//catch (Exception e)
//{
// strOutput = e.Message;
//}
undoImpersonation();
return strOutput;
}
else return "log failed;";
}
protected void Button1_Click(object sender, EventArgs e)
{
//Insert your code that runs under the security context of a specific user here.
string s = "";
s = ExeCommand("E:\\WinRAR\\rar a E:\\test\\1." + this.DropDownList1.SelectedValue + ".rar E:\\test\\1." + this.DropDownList1.SelectedValue);
Response.Write(s);
}
</SCRIPT>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>doc</asp:ListItem>
<asp:ListItem>docx</asp:ListItem>
<asp:ListItem>xls</asp:ListItem>
<asp:ListItem>xlsx</asp:ListItem>
<asp:ListItem>ppt</asp:ListItem>
<asp:ListItem>pptx</asp:ListItem>
<asp:ListItem>pdf</asp:ListItem>
</asp:DropDownList>
<p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"
style="margin-top: 0px" />
</p>
</form>
</body>
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------
尝试模拟时发生错误。无法继续执行此请求。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Web.HttpException: 尝试模拟时发生错误。无法继续执行此请求。
源错误:
执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。
堆栈跟踪:
[HttpException (0x80004005): 尝试模拟时发生错误。无法继续执行此请求。]
System.Web.ImpersonationContext.GetCurrentToken() +8845961
System.Web.ImpersonationContext.get_CurrentThreadTokenExists() +58
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +193
System.Web.ApplicationStepManager.ResumeSteps(Exception error) +501
System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +123
System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +379
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.3053; ASP.NET 版本:2.0.50727.3053