访问网络驱动器ASP网络核心上的文件

时间:2020-08-20 14:13:46

标签: asp.net-core

我通过asp核心终结点成功打开文件:

        [HttpGet("files/{fileName}")]
        public IActionResult GetFile(string fileName)
        {
            var filePath = _hostingEnvironment.ContentRootPath + "\\Files\\" + fileName;
            if (filePath == null) return NotFound();
            return PhysicalFile(filePath, MimeTypes.GetMimeType(filePath));
        }

该文件位于服务器本地文件夹中

C:\inetpub\wwwroot\myProject\files

我要对安装为磁盘R:\的网络文件夹执行相同的操作:

var filePath = "R:\\Files\\" + fileName;
R:\files

但这不适用于错误500。 我也可以使用登录名和密码直接通过Windows资源管理器从R:\打开文件。 那么如何访问网络驱动器文件?

1 个答案:

答案 0 :(得分:0)

您需要获取映射到R:Drive的网络地址,然后可以将其用作代码中的文件/文件夹路径:

由于您需要密码才能访问此共享驱动器中的文件,因此您有两个选择,如本SO answer

中所述

1:设置AppPool用户

执行此操作的“正确”方法是将网络服务器的AppPool作为 可以访问共享的身份。这样,唯一的凭证 存储在IIS配置中安全完成(而不是在代码中) 或可读的配置文件中)。将网络服务器和文件服务器放入 相同的Windows域(或具有信任关系的不同域)是 最简单的方法,但是“相同的用户名/密码”应该在那里 也是

2:P /调用WNetAddConnection2 How To Access Network Drive Using C#

在这里实现良好
using System;  
using System.ComponentModel;  
using System.Runtime.InteropServices;  
using System.Net; 
public class ConnectToSharedFolder: IDisposable  
{  
    readonly string _networkName;  
  
    public ConnectToSharedFolder(string networkName, NetworkCredential credentials)  
    {  
        _networkName = networkName;  
  
        var netResource = new NetResource  
        {  
            Scope = ResourceScope.GlobalNetwork,  
            ResourceType = ResourceType.Disk,  
            DisplayType = ResourceDisplaytype.Share,  
            RemoteName = networkName  
        };  
  
        var userName = string.IsNullOrEmpty(credentials.Domain)  
            ? credentials.UserName  
            : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);  
  
        var result = WNetAddConnection2(  
            netResource,  
            credentials.Password,  
            userName,  
            0);  
  
        if (result != 0)  
        {  
            throw new Win32Exception(result, "Error connecting to remote share");  
        }  
    }  
  
    ~ConnectToSharedFolder()  
    {  
        Dispose(false);  
    }  
  
    public void Dispose()  
    {  
        Dispose(true);  
        GC.SuppressFinalize(this);  
    }  
  
    protected virtual void Dispose(bool disposing)  
    {  
        WNetCancelConnection2(_networkName, 0, true);  
    }  
  
    [DllImport("mpr.dll")]  
    private static extern int WNetAddConnection2(NetResource netResource,  
        string password, string username, int flags);  
  
    [DllImport("mpr.dll")]  
    private static extern int WNetCancelConnection2(string name, int flags,  
        bool force);  
  
    [StructLayout(LayoutKind.Sequential)]  
    public class NetResource  
    {  
        public ResourceScope Scope;  
        public ResourceType ResourceType;  
        public ResourceDisplaytype DisplayType;  
        public int Usage;  
        public string LocalName;  
        public string RemoteName;  
        public string Comment;  
        public string Provider;  
    }  
  
    public enum ResourceScope : int  
    {  
        Connected = 1,  
        GlobalNetwork,  
        Remembered,  
        Recent,  
        Context  
    };  
  
    public enum ResourceType : int  
    {  
        Any = 0,  
        Disk = 1,  
        Print = 2,  
        Reserved = 8,  
    }  
  
    public enum ResourceDisplaytype : int  
    {  
        Generic = 0x0,  
        Domain = 0x01,  
        Server = 0x02,  
        Share = 0x03,  
        File = 0x04,  
        Group = 0x05,  
        Network = 0x06,  
        Root = 0x07,  
        Shareadmin = 0x08,  
        Directory = 0x09,  
        Tree = 0x0a,  
        Ndscontainer = 0x0b  
    }  
}  

public string networkPath = @"\\{Your IP or Folder Name of Network}\Shared Data";  
NetworkCredential credentials = new NetworkCredential(@"{User Name}", "{Password}");  
public string myNetworkPath = string.Empty;

public byte[] DownloadFileByte(string DownloadURL)  
{  
    byte[] fileBytes = null;  
  
    using (new ConnectToSharedFolder(networkPath, credentials))  
    {  
        var fileList = Directory.GetDirectories(networkPath);  
  
        foreach (var item in fileList) { if (item.Contains("ClientDocuments")) { myNetworkPath = item; } }  
  
        myNetworkPath = myNetworkPath + DownloadURL;  
  
        try  
        {  
            fileBytes = File.ReadAllBytes(myNetworkPath);  
        }  
        catch (Exception ex)  
        {  
            string Message = ex.Message.ToString();  
        }  
    }  
  
    return fileBytes;  
}