在ASP.Net程序中,我在程序中使用WNetAddConnection2映射网络磁盘。再启动另一个进程调用外部EXE使用映射的网络磁盘资源。当在Web.config中设置
   <identity impersonate="true"
            userName="Administrator"
            password="pass"
    /> 
时,外部EXE执行失败。无法取得映射的网络磁盘资源。如果不在Web.config中设置
则可以正常运行。问题:
1、为什么会有此现象?
2、怎样使程序正常执行?(需要在Web.config中设置。)
以下是本人的推测:(完全个人意见,无任何根据。希望不要影响各位大侠)
1、使用模拟用户登陆后,建立的磁盘映射只对当前进程中的当前模拟用户有效(即使模拟用户与windows登陆用户一致),所以“调用的”外部EXE无法取得模拟用户建立的磁盘映射资源。
不使用模拟用户时,使用的是当前windows登陆用户,建立的磁盘映射对windows登陆用户有效,所以“调用的”外部EXE使用windows登陆用户可以获取映射磁盘的资源。(此处感觉不是,不模拟登陆时应该是ASP.net用户吧,那为什么建立的映射在“调用的”外部EXE中有效)
2、另做个EXE,在另一个进程中建立磁盘映射。此时应当是windows用户。(结果:在ASP.net程序中调用此EXE,显示的磁盘映射为不可用的。单独执行EXE磁盘映射可用。)

解决方案 »

  1.   

    难道没人研究过么?搞了好久才知道是Web.config设置的模拟用户问题。期待高手中!!版主!救命!
      

  2.   

    可能是权限不够引起的,
    你看看用asp.net的process运行dos命令映射试试看能不能成功.
      

  3.   

    我写了示例代码:首先,创建一个控制台程序FileReader,将下面代码加入到Main中
    [STAThread]
    static void Main(string[] args)
    {
    if(args.Length != 1)
    {
    System.Console.WriteLine("参数错误。请指定文件路径!");
    return;
    } try
    {
    string path = args[0];
    StreamReader sr = File.OpenText(path);
    while(sr.Peek() >= 0)
    {
    System.Console.WriteLine(sr.ReadLine());
    }
    }
    catch(System.Exception ex)
    {
    System.Console.WriteLine(ex.Message);
    }
    }另外,创建一个asp.net程序,将下面代码加入
    Page_Load中private void Page_Load(object sender, System.EventArgs e)
    {
    string remote = @"\\server\share"; //<--网络共享文件夹
    string user = "user";               //<--登陆网络用户名
    string pass = "pass";              //<--登陆密码
    string file = @"m:\my.txt";        //<--共享文件夹内文件
    string drive = "m:";               //<--映射为的本地磁盘名 StringBuilder sbContent = new StringBuilder();
    sbContent.Append("<pre>\r\n"); // builde net mapped
    stNetResource nr;
    nr.iScope = 2;
    nr.iType = 1;
    nr.iDisplayType = 3;
    nr.iUsage = 1;
    nr.strLocalName = drive;
    nr.strRemoteName = remote;
    nr.strComment = null;
    nr.strProvider = null; int mm = WNetAddConnection2(ref nr,pass,user,0);
    Win32Exception ex = new System.ComponentModel.Win32Exception(mm);
    sbContent.Append(ex.Message);
    sbContent.Append("\r\n"); #region operate Process operate = new Process(); operate.StartInfo.FileName = "FileReader.exe"; operate.StartInfo.Arguments = file; operate.StartInfo.UseShellExecute = false; operate.StartInfo.RedirectStandardInput = true; operate.StartInfo.RedirectStandardOutput = true; operate.StartInfo.RedirectStandardError = true; operate.StartInfo.CreateNoWindow = true; operate.Start();  while (operate.StandardOutput.Peek() >= 0) 
    {
    sbContent.Append(operate.StandardOutput.ReadLine());
    sbContent.Append("\r\n");
    } operate.Dispose();
    #endregion mm = WNetCancelConnection2(drive,0,true);
    ex = new System.ComponentModel.Win32Exception(mm);
    sbContent.Append(ex.Message);
    sbContent.Append("\r\n"); sbContent.Append("</pre>"); this.Response.ContentEncoding = System.Text.Encoding.Default;
    this.Response.Write( sbContent.ToString() );}在引入下面的API
    //cancel net mapped
    [DllImport("mpr.dll")]
    private static extern int WNetCancelConnection2(
    string strResourceName,
    int connectionType,
    bool bForce
    );
    //builde net mapped
    [DllImport("mpr.dll")]
    private static extern int WNetAddConnection2(
    ref stNetResource netResource,
    string strPassword,
    string strUserName,
    int iFlags
    );
    [StructLayout(LayoutKind.Sequential)]
    private struct stNetResource
    {
    public int iScope;
    public int iType;
    public int iDisplayType;
    public int iUsage;
    public string strLocalName;
    public string strRemoteName;
    public string strComment;
    public string strProvider;
    }再在web.config中添加   <identity impersonate="true"
                userName="Administrator" <--你的用户
                password="pass"          <--你的密码
        /> 当在web.config添加-->失败
    否则,可以读去文件内容并显示。
    我要做的是:
    在asp.net程序中映射一个网络磁盘,然后调用一个EXE使用此网络磁盘。
    但是项目要求设置web.config模拟用户。注:使用DOS的use net命令不能完全满足要求。还有我想知道为什么,windows在各时刻处理模拟的哪个用户。