当前位置:编程学习 > C#/ASP.NET >>

ASP.net 访问映射到本地的网络驱动器,权限错误,IIS6 没问题,IIS7权限找不到怎么设置

从网络上映射了一个网络文件夹到本地后,用asp.net 访问,总是报错 找不到Z:\XXX.txt 文件。代码是原先在IIS6.0下写的,配置了安全选项能够正常访问,但是到了IIS7.5后找不到的对应的安全选项设置。总是在vs2008里正常访问到了IIS下就不能访问了配置,仅配置<identity impersonate="true" password="XXX" userName="***" />vs2008 iis都访问不成功。
问题究竟出在哪里呢?请大家帮我分析分析……
我尝试使用代码创建的映射网络驱动器,能够访问正常,而用windows创建的访问就不正常了。
因此暂时想出来个解决方法,用代码分析windows创建的映射磁盘,获得对应的网络共享文件夹路径,然后用这个文件路径访问资源。
代码如下:给大家一个参考

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Xml; 

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //获取映射磁盘对应的共享路径
            string Sharepath = Pathing.GetUNCPath(@"z:\"); // path = "\\\\cjsj\\share"
            //string s = System.Security.Principal.WindowsIdentity.GetCurrent().Name;\\测试当前访问的用户名(本机OR文件服务器用户)

            File.Copy(Sharepath+"\\123.txt", @"d:\123.txt",true);//使用获取的对应共享文件夹路径访问
            Console.Write(Sharepath);
        }

    }
    public static class Pathing  
    {  
        [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]  
        public static extern int WNetGetConnection(  
            [MarshalAs(UnmanagedType.LPTStr)] string localName,  
            [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName,  
            ref int length);  
        /// <summary>   
        /// 给定一个路径,返回的网络路径或原始路径。    
        /// 例如:给定路径 P:\2008年2月29日(P:为映射的网络驱动器名),可能会返回:“\\networkserver\照片\2008年2月9日”   
        /// </summary>   
        /// <param name="originalPath">指定的路径</param>   
        /// <returns>如果是本地路径,返回值与传入参数值一样;如果是本地映射的网络驱动器</returns>   
        public static string GetUNCPath(string originalPath)  
        {  
            StringBuilder sb = new StringBuilder();  
            int size = sb.Capacity;  
            if (originalPath.Length > 2 && originalPath[1] == ':')  
            {  
                char c = originalPath[0];  
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))  
                {  
                    int error = WNetGetConnection(originalPath.Substring(0, 2),  
                        sb, ref size);  
                    if (error == 0)  
                    {  
                        DirectoryInfo dir = new DirectoryInfo(originalPath);  
                        string path = Path.GetFullPath(originalPath)  
                            .Substring(Path.GetPathRoot(originalPath).Length);  
                        return Path.Combine(sb.ToString().TrimEnd(), path);  
                    }  
                }  
            }  
            return originalPath;  
        }  
    }
}


代码创建网络映射磁盘如下:(程序创建的我能访问,windows创建的我不能访问why!!!)

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Xml; 

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           WNetHelper.WNetAddConnection(@"文件服务器机器名\用户名", "密码", Sharepath, "s:");
           //sharepath  共享文件路径
            File.Copy(@"s:\123.txt", @"d:\123.txt",true);
        }

    }

  public class WNetHelper
    {

        [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]

        private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags);



        [DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2")]

        private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce);



        [StructLayout(LayoutKind.Sequential)]

        public class NetResource
        {

            public int dwScope;



            public int dwType;



            public int dwDisplayType;



            public int dwUsage;



            public string lpLocalName;



            public string lpRemoteName;



            public string lpComment;



            public string lpProvider;

        }



        /// <summary>  

        /// 为网络共享做本地映射  

        /// </summary>  

        /// <param name="username">访问用户名(windows系统需要加计算机名,如:comp-1\user-1)</param>  

        /// <param name="password">访问用户密码</param>  

        /// <param name="remoteName">网络共享路径(如:\\192.168.0.9\share)</param>  

        /// <param name="localName">本地映射盘符</param>  

        /// <returns></returns>  

        public static uint WNetAddConnection(string username, string password, string remoteName, string localName)
        {

            NetResource netResource = new NetResource();



            netResource.dwScope = 2;

            netResource.dwType = 1;

            netResource.dwDisplayType = 3;

            netResource.dwUsage = 1;

            netResource.lpLocalName = localName;

            netResource.lpRemoteName = remoteName.TrimEnd('\\');

            uint result = WNetAddConnection2(netResource, password, username, 0);



            return result;

        }



        public static uint WNetCancelConnection(string name, uint flags, bool force)
        {

            uint nret = WNetCancelConnection2(name, flags, force);



            return nret;

        }
    }
}
--------------------编程问答-------------------- 还是老问题,权限
安全选项设置 肯定是有的 --------------------编程问答-------------------- 之前我们的做法是在两台机器上设置相同的用户名和密码

这样的话你配置的用户名密码就有访问权限了 --------------------编程问答--------------------
引用 2 楼 on1y_1onely 的回复:
之前我们的做法是在两台机器上设置相同的用户名和密码

这样的话你配置的用户名密码就有访问权限了

机器名不用管吗 ? 用户名密码确实是配置在web.config中吗?
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,