简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM