2

I have a shared path \\mynetworkshare\myfolder which has images stored.

I have a domain user mydomainuser which was granted with read access to files in that path

I hosted website on IIS. I created a Virtual Directory within my IIS Site with an Alias myphotos pointing to the Physical path \\mynetworkshare\myfolder. I have also clicked on Connect as... button in the Add Virtual Directory dialog box and provided my mydomainuser credentials.

I clicked on the newly created virtual director myphotos and click on Content View in the right pane. I'm able to view all my photos within IIS. From this, I assume the setup of virtual directory to my shared drive is correct.

Now, the question is how do I access this Virtual Directory or Files in it from my code?

I have tried below

var filePath = Server.MapPath("~/myphotos/" + "myimage.jpg");

When I write the filePath to a log file, I see it is trying to map to a physical folder setup within my website folders.

Instead of pointing to

\\mynetworkshare\myfolder\myimage.jpg

it is pointing to

d:\wwwroot\inetpub\mywebsitefolder\myphotos\myimage.jpg

I know Server.MapPath resolves to a physical path of hosted site but I wonder if it behaves the same with my virtual directory.

Or Do I need to let ASP.Net know somehow that myphotos is a virtual directory created on IIS? Or Am I on the wrong path to get files? Do I need to write code something different?

techspider
  • 3,370
  • 13
  • 37
  • 61
  • We ended up using the web.config AppSettings to set the unc path to the files folder, then just append the sub folder/file name in code. My instinct says that if the aspx page calling the Server.MapPath was on the UNC path also, then it would read the correct UNC path. – Dr. Aaron Dishno Feb 01 '16 at 18:14
  • I can't UNC directly in code as it is restrictive; how do I impersonate with mydomainuser for UNC? – techspider Feb 01 '16 at 18:17
  • impersonate, there are a number of stackoverflow answers on that subject. depends on web service, trusts, authentication, etc... Maybe this bit of code could be useful.. http://stackoverflow.com/questions/659013/accessing-a-shared-file-unc-from-a-remote-non-trusted-domain-with-credentials – Dr. Aaron Dishno Feb 01 '16 at 18:26
  • @Dr.AaronDishno, I have found the issue with my setup; and I'm able to read files; posted as answer – techspider Feb 02 '16 at 16:53

2 Answers2

0

You can get the physical path from IIS (7+) using System.Web.Administration (available in NuGet)

var physicalPath = new Microsoft.Web.Administration.ServerManager()
    .Sites["Default Web Site"]
    .Applications["/MyApplication"]
    .VirtualDirectories["/MyVirtualPath"]
    .PhysicalPath;

If the virtual directory is in the root, the Application is "/"

What's specified in the Connect as... may only apply to direct web requests. To access the files from your application, you will need to configure the Application Pool to use mydomainuser as its Identity (found under Advanced Settings...), or you will need to grant share permissions to the computer running IIS if the Application pool uses a built-in account.

bdimag
  • 953
  • 8
  • 11
  • This is just not about accessing the physical path but having the ability to read the files in the virtual folder using certain credentials setup already on virtual folder – techspider Feb 01 '16 at 18:53
0

The step that was missing is converting Virtual Folder as Application.

Right-click the Virtual Folder myphotos, click on Convert to Application

The link here should explain the steps

Without adding Server.MapPath, it works fine. For example

image.src = "~/myphotos/myimage.jpg"
techspider
  • 3,370
  • 13
  • 37
  • 61