MDspb
MDspb

Reputation: 45

Access to file on network drive asp net core

I open file by asp core endpoint successfully:

        [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));
        }

The file is in the server local folder:

C:\inetpub\wwwroot\myProject\files

I want to do the same with network folder mounted as disk R:\:

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

But this do not works with error 500. I also can open file from R:\ directly by windows explorer with login & password. So how to get access to the network drive file?

Upvotes: 0

Views: 11451

Answers (1)

Aman B
Aman B

Reputation: 2398

You'll need to get the network address which is mapped to R: Drive, then you can use it as the file/folder path in your code:

Since you require a password to access the files in this shared drive, you've got two options as explained in this SO answer

1: Set AppPool user

The "right" way to do this is to run the webserver's AppPool as the identity that can access the share. That way, the only credential storage is done securely in the IIS config (rather than in your code or in readable config files). Putting the webserver and fileserver in the same Windows domain (or different domains with trust) is the easiest way, but the "same username/password" thing should work there as well.

2: P/Invoke to WNetAddConnection2 This has a good implementation here 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;  
}  

Upvotes: 1

Related Questions