想通过web方式在服务器端执行bat,主要涉及到模拟权限问题.搞了2天,没搞清楚模拟权限怎么搞.哪位可以指点2句~~~~

解决方案 »

  1.   

    参考
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Web.Security;
    using System.Security.Principal;
    using System.Runtime.InteropServices;
    namespace Security
    {
    /// <summary>
    /// 模拟
    /// </summary>
    public class ASPNETImp : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label lbExist;
    protected System.Web.UI.WebControls.Label Label2;
    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);
    private void Page_Load(object sender, System.EventArgs e)
    {
    //noImpersonate();
    //ImpersonateIIS();
    ImpersonateUser();
    }
    private void noImpersonate()
    {
    try
    {
    if(File.Exists("c:\\Documents and Settings\\shaozhidong\\test.txt"))
    lbExist.Text = "存在!";
    else
    lbExist.Text = "该文件不存在!";
    }
    catch(Exception)
    {
    lbExist.Text = "没有权限!";
    }
    }
    private void ImpersonateIIS()
    {
    // 在代码中模拟IIS认证帐号
    System.Security.Principal.WindowsImpersonationContext impersonationContext;
    impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate(); if(File.Exists("c:\\Documents and Settings\\shaozhidong\\test.txt"))
    lbExist.Text = "存在!";
    else
    lbExist.Text = "该文件不存在!";

    impersonationContext.Undo();
    }
    private void ImpersonateUser()
    {
    //在代码中模拟指定账号
    if(impersonateValidUser("shaozhidong", "shaozhd", "111"))
    {   
    if(File.Exists("c:\\Documents and Settings\\shaozhidong\\test.txt"))
    lbExist.Text = "存在!";
    else
    lbExist.Text = "该文件不存在!";
    undoImpersonation();
    }
    else
    {
    lbExist.Text = "权限不够!";
    }
    }
    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();
    }  #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    }
    }
      

  2.   

    我在文件存在的时候,并且有权限的情况下.执行 System.Diagnostics.Process.Start(@"H:\1.bat");
    没反映,why?
      

  3.   

    可以建文件,刪除修改文件。就是不能執行bat文件。點解呢
      

  4.   

    应该是可以执行 bat 但是 bat 中的某些命令权限不足你可以在bat中第一句上加上一句echo %time% > c:\test.txt把 c:\test.txt 设为everyone 可读写
      

  5.   

    你可以看到 bat 文件能创建一个文件说明 bat 有执行
      

  6.   

    window + IIS 权限是个繁琐的东西Process proc = new Process();
    proc.Exited += new EventHandler(proc_Exited);
    proc.StartInfo.FileName = "cmd.exe"; 
    proc.StartInfo.WorkingDirectory = AppFolder; 
    proc.StartInfo.CreateNoWindow = true; 
    proc.StartInfo.UseShellExecute = false;  proc.StartInfo.RedirectStandardInput = true; //输入重定向 
    proc.StartInfo.RedirectStandardOutput = true; //标准输出重定向  proc.Start(); 
    proc.StandardInput.WriteLine(sExecuteCommand); 
    Thread.Sleep(new System.TimeSpan(0, 0, 0, 5));
    proc.StandardInput.WriteLine("exit"); 

    sExecResult = proc.StandardOutput.ReadToEnd(); //关闭前返回脚本执行的结果
    proc.WaitForExit();以前在 win2000 + iis5通过
    在 win2003+ iis6 就报权限问题, 几乎所有可能的权限都设了, 还是不行,  =_="
      

  7.   

    写了个组件,单个执行时,调用组件执行。
    下面是主要代码:
    Param类是参数类
                      public string flvstr;
    public string flvmdi;
    Param P = new Param(); public Flv()
    {
    this.flvstr = ConfigurationSettings.AppSettings["flv"];
    this.flvmdi = ConfigurationSettings.AppSettings["mdi"];
    } public void VideoConvertFlv(string VideoName,string FormPath,string ToPath)
    {
    Process o = new Process();
    string text1 = base.Server.MapPath(this.flvstr);
    o.StartInfo = new ProcessStartInfo(text1);
    o.StartInfo.Arguments =  P.mencoder(VideoName,FormPath,ToPath);
    o.Start();
    o.WaitForExit();
    Xiu(VideoName,ToPath);
    } private void Xiu(string VideoName,string ToPath)
    {
    Process r = new Process();
    string text2 = base.Server.MapPath(this.flvmdi);
    r.StartInfo = new ProcessStartInfo(text2);
    r.StartInfo.Arguments = P.flvmdi(ToPath,VideoName);
    r.Start();
    r.WaitForExit();
    }