Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...