ASP.NET网络映射驱动器无权限访问的解决方案

             Windows的网络驱动器映射能加网络上的地址映射为本地磁盘(如Z:盘),可以在资源管理器点“映射网络驱动器”或者cmd下输subst命令进行操作;这在许多时候是十分有用的,比如程序要访问其他厂商的存储在文件服务器上的图片、文档等,这时候通过网络映射就在程序中就能向本地文件一样访问网络上其他位置的文件,同时一定程序也简化了编程。

          但在ASP.NET运行在IIS上时,运行的账户是NETWORK SERVICE或AUTHENTICATED USERS的权限,这就导致没有权限访问映射的驱动器,如下面的代码将会出现FileNotFound的异常。

System.Drawing.Image image = System.Drawing.Image.FromFile("Z://1.jpg");
image.Save("c:\\1.jpg");     

          这个问题在我一年前就遇到过,当时没有解决,也找了很多网络上的资料,许多都描述不是太清楚,有建虚拟目录,有修改组策略的,有用户模拟的,网络上跟我一样求助的人一大把,后来摸索了下,整理下思路就把这个问题解决了。

          首先,是需要用户模拟的,但不代表可以获取访问网络磁盘的权限。

 <identity impersonate="true" userName="administrator" password="admin"/>

           然后,需要调用WNetAddConnection2与网络资源建立连接,WNetCancelConnection2A断开连接,WNetAddConnection2的MSDN解释是:The WNetAddConnection2 function makes a connection to a network resource. The function can redirect a local device to the network resource。网络上找得一个NetworkConnection封装类进行P\Invoke调用,贴代码如下:

   public enum ERROR_ID
    {
        ERROR_SUCCESS = 0,  // Success
        ERROR_BUSY = 170,
        ERROR_MORE_DATA = 234,
        ERROR_NO_BROWSER_SERVERS_FOUND = 6118,
        ERROR_INVALID_LEVEL = 124,
        ERROR_ACCESS_DENIED = 5,
        ERROR_INVALID_PASSWORD = 86,
        ERROR_INVALID_PARAMETER = 87,
        ERROR_BAD_DEV_TYPE = 66,
        ERROR_NOT_ENOUGH_MEMORY = 8,
        ERROR_NETWORK_BUSY = 54,
        ERROR_BAD_NETPATH = 53,
        ERROR_NO_NETWORK = 1222,
        ERROR_INVALID_HANDLE_STATE = 1609,
        ERROR_EXTENDED_ERROR = 1208,
        ERROR_DEVICE_ALREADY_REMEMBERED = 1202,
        ERROR_NO_NET_OR_BAD_PATH = 1203
    }

    public enum RESOURCE_SCOPE
    {
        RESOURCE_CONNECTED = 1,
        RESOURCE_GLOBALNET = 2,
        RESOURCE_REMEMBERED = 3,
        RESOURCE_RECENT = 4,
        RESOURCE_CONTEXT = 5
    }

    public enum RESOURCE_TYPE
    {
        RESOURCETYPE_ANY = 0,
        RESOURCETYPE_DISK = 1,
        RESOURCETYPE_PRINT = 2,
        RESOURCETYPE_RESERVED = 8,
    }

    public enum RESOURCE_USAGE
    {
        RESOURCEUSAGE_CONNECTABLE = 1,
        RESOURCEUSAGE_CONTAINER = 2,
        RESOURCEUSAGE_NOLOCALDEVICE = 4,
        RESOURCEUSAGE_SIBLING = 8,
        RESOURCEUSAGE_ATTACHED = 16,
        RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
    }

    public enum RESOURCE_DISPLAYTYPE
    {
        RESOURCEDISPLAYTYPE_GENERIC = 0,
        RESOURCEDISPLAYTYPE_DOMAIN = 1,
        RESOURCEDISPLAYTYPE_SERVER = 2,
        RESOURCEDISPLAYTYPE_SHARE = 3,
        RESOURCEDISPLAYTYPE_FILE = 4,
        RESOURCEDISPLAYTYPE_GROUP = 5,
        RESOURCEDISPLAYTYPE_NETWORK = 6,
        RESOURCEDISPLAYTYPE_ROOT = 7,
        RESOURCEDISPLAYTYPE_SHAREADMIN = 8,
        RESOURCEDISPLAYTYPE_DIRECTORY = 9,
        RESOURCEDISPLAYTYPE_TREE = 10,
        RESOURCEDISPLAYTYPE_NDSCONTAINER = 11
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct NETRESOURCE
    {
        public RESOURCE_SCOPE dwScope;
        public RESOURCE_TYPE dwType;
        public RESOURCE_DISPLAYTYPE dwDisplayType;
        public RESOURCE_USAGE dwUsage;

        [MarshalAs(UnmanagedType.LPStr)]
        public string lpLocalName;

        [MarshalAs(UnmanagedType.LPStr)]
        public string lpRemoteName;

        [MarshalAs(UnmanagedType.LPStr)]
        public string lpComment;

        [MarshalAs(UnmanagedType.LPStr)]
        public string lpProvider;
    }

    public class NetworkConnection
    {
        [DllImport("mpr.dll")]
        public static extern int WNetAddConnection2A(NETRESOURCE[] lpNetResource, string lpPassword, string lpUserName, int dwFlags);

        [DllImport("mpr.dll")]
        public static extern int WNetCancelConnection2A(string sharename, int dwFlags, int fForce);

        public static int Connect(string remotePath, string localPath, string username, string password)
        {
            NETRESOURCE[] share_driver = new NETRESOURCE[1];
            share_driver[0].dwScope = RESOURCE_SCOPE.RESOURCE_GLOBALNET;
            share_driver[0].dwType = RESOURCE_TYPE.RESOURCETYPE_DISK;
            share_driver[0].dwDisplayType = RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SHARE;
            share_driver[0].dwUsage = RESOURCE_USAGE.RESOURCEUSAGE_CONNECTABLE;
            share_driver[0].lpLocalName = localPath;
            share_driver[0].lpRemoteName = remotePath;

            Disconnect(localPath);
            int ret = WNetAddConnection2A(share_driver, password, username, 1);

            return ret;
        }

        public static int Disconnect(string localpath)
        {
            return WNetCancelConnection2A(localpath, 1, 1);
        }
    }

            最后,就是使用了,其中Connect方法的参数分别为网络地址、本地要映射的磁盘、网络映射账号、网络映射密码

    string localpath = "Z:";
    int status = NetworkConnection.Connect("\\\\192.168.1.111\\share", localpath, @"administrator", "admin");
    if (status == (int)ERROR_ID.ERROR_SUCCESS)
    {
    System.Drawing.Image image = System.Drawing.Image.FromFile("Z://1.jpg");
    image.Save("c:\\1.jpg");     
    }
    NetworkConnection.Disconnect(localpath);

           至于为什么要这样,我的理解是直接调用API建立连接可能也没有权限,在此需要模拟一下用户,再建立连接就直接可以访问,相当于ASP.NET运行的账户下做了网络磁盘映射。

posted @ 2011-10-09 19:31  小胖2010  阅读(4454)  评论(8编辑  收藏  举报