Jecoms
Jecoms

Reputation: 2848

Read Files from Remote Drive in Intranet App

I want to read file names of pdfs from a folder on a network share that match certain parameters and provide a link to view them on a details page for my model. All I need to get is the file name. I don't need any file management/read/write at this time.

I'm able to display a .pdf in the browser if I have the path (IE will open "file://" links). The part I'm missing is getting file names from the remote (but same domain) directory at run-time.

We've set up a virtual directory for the app to use and that has worked fine in the past if the resolved physical folder is on the same server, but that is not the case here.

I've tried Impersonation, but that doesn't seem to work as I'm still getting an access is denied error.

I realize this would probably be a security issue and is why it isn't allowed, but is there an IIS configuration or other avenue that needs to be set-up to allow this? I can't seem to find a way with just code that opens the directory for reading.

Example code of how I might normally read some info from one file in a virtual directory:

// This example code is inside a controller action, so Server refers to HttpContextBase
var path = Server.MapPath("~/MyVirtualDirectory/" + fileName);
var fileExists = System.IO.File.Exists(path);
var fileLastModified = System.IO.File.LastWriteTime(path);

To enumerate over matching files in a directory, I've used DirectoryInfo

var pdfFileNames = new List<string>();
var dir = new DirectoryInfo(Server.MapPath("~/VirtualDirectory/"));
var pdfs = dir.EnumerateFiles("*.pdf");
foreach (var pdf in pdfs) 
{
    pdfFileNames.Add(pdf.Name);
}

As I mentioned, these methods work fine when the physical folder is on the same server, but once the directory is on a remote drive, then it no longer works. I have permissions to open the desired directory and my collegue said he gave the appropriate permissions to the virtual directory and server. Not sure what else to try at this point.

Edit: Now that it is working, I display the files using the Virtual Directory

http://server/appName/virtualDirectory/pdfFileName

Upvotes: 0

Views: 1001

Answers (1)

Mathieu Renda
Mathieu Renda

Reputation: 15366

By default, IIS application pools run under a specific local Windows identity named IIS APPPOOL\[NameOfYourAppPool]. This is a local user and it will not be possible to grant permissions to this identity to access resources located on a different machine.

If both servers are inside the same domain, you can try the following solutions:

  • Run the IIS application under a domain user and grant the required permissions to this domain user.
  • Run the IIS application under the NetworkService identity and grant permissions to the DOMAIN\MACHINENAME$ account of the IIS server.

Upvotes: 1

Related Questions