Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access mapped drive by IIS running asp.net mvc web application

I have deployed an asp.net mvc web application on IIS 8 server inside an virtual machine (Windows server 2012 R2). An azure file storage is mapped as network drive in this virtual machine Windows server 2012 R2. Now my asp.net mvc web application needs to read the files and folders of this mapped drive by C# System.IO code. By default IIS is not allowed to access mapped drives.

That's why the web application is throwing System.IO exception

"Could not find the specified path Z:\"

. I also tried to pass "\\\\{storage-name}.file.core.windows.net\\{fileshare-name} but still no success.

Can some one guide me with correct configurations and settings which I should do inside IIS and web application?

like image 559
Kishan Gupta Avatar asked Feb 15 '16 15:02

Kishan Gupta


People also ask

How do I access a network drive in IIS?

In the IIS Manager , select the Application Pool under which your Web Site is running. Click "Advanced Settings". There will be an entry for Identity (it is under the Process Model section). Click it, provide credentials for your account that has permission to access the share.

Can a Windows Service access a mapped drive?

As a rule of thumb Windows services can't access mapped drives and you have to use UNC paths instead.

Can you Map a network drive to a server?

Select Map Network Drive from the Tools menu. Select the drive that you want to use to connect to your server. Type the path name to your server. For example, \\MYSERVER where MYSERVER is the name of your server.


1 Answers

Finally, I succeeded to access the mapped network drive through IIS Server. I performed following steps.

  • Create a new user account on VM.
  • The user name for this new account will be the Storage Account Name
  • The password for this user will be Storage account key which ends with "=="
  • After creating this new user account I changed the account type for this user to Administrator
  • Go to My Computer OR This PC
  • Attach the network drive with the help of Map network drive.. option.
  • Open IIS Manager window, go to Application Pools
  • Select the application pool which is being used by your web application (in my case it was DefaultAppPool and click on Advanced Settings... from right side pane.
  • Change the Identity for this application pool with newly created user account name and password.
  • Set Load User Profile to true.
  • Click OK to save changes.
  • Click on Recycle link from right side pane to refresh the selected application pool.
  • Now select your web application which is under Default We Site.
  • Click on Basic Settings... to open Edit Site dialog box.
  • Make sure that the Application Pool name is correct.
  • Click on Connect as... button and select Specific user radio button and then set the credentials with this newly created User name (Storage account name) and password (Storage account key).

That's it. Now you can simply write standard C# IO code to access the directory and files of mapped drive. Here is a sample example.

var allDirs = Directory.GetDirectories("\\\\<storageaccountname>.file.core.windows.net\\<storagefileshare>");
ViewBag.Items = allDirs;

Make sure that you access the files and folders by UNC path format only, just like I have done in above code.

like image 65
Kishan Gupta Avatar answered Oct 22 '22 12:10

Kishan Gupta