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

15,882

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.

Share:
15,882
Kishan Gupta
Author by

Kishan Gupta

I am working as a Technical Manager in a software company. I have total experience of 5+ years in .NET. Here are my Microsoft certifications: MCSD Web 4.0 MOS Excel 2010

Updated on June 28, 2022

Comments

  • Kishan Gupta
    Kishan Gupta almost 2 years

    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?

  • user1403598
    user1403598 over 5 years
    Personally i think this is a dangerous method, there is a reason for why you use ApplicationPools. Using an administrator account for a web application is generally a no go for security reasons. If any malicious code was to be executed then you could wipe out a server.
  • Junior Grão
    Junior Grão over 3 years
    Hi @Kishan Gupta, it helped me a lot. In my case I just changed the user type from "Administrator" to "Standard User" and it worked too. So, for my project I could skip this step: "After creating this new user account I changed the account type for this user to Administrator" Thanks
  • Baccata
    Baccata about 2 years