Click here to Skip to main content
15,881,172 members
Articles / Web Development / IIS
Article

How to Access Network Files using asp.net

Rate me:
Please Sign up or sign in to vote.
3.45/5 (20 votes)
1 Aug 20074 min read 245.4K   50   17
Here i am showing, how user can access network files and folder that are stored on other Server.

Introduction

Here I am Showing How to Access Network Files and Folder using asp.net

Background

When we Develop web application to dealing with Files and folders, at that time our web server is not quite capable to manage files and folder, so in this scenario we can use two different server to manage application properly, so we need Web Server to host our web application and File Server to manage files and folder.

To implement that scenario In Web application, there are so many ways to do that. Here I am showing it using Synchronize the IUSR account between the two machines. We can do using Asp.net Impersonate. We can implement asp.net impersonate in different way, mean by programmatically or using web.config file.

Synchronize the IUSR account between the two machines

Here is list of steps that you have to follow to achieve it.

  1. Setup IIS to Allow us to Use Network files and Folder
  2. Enable Impersonate in Asp.net.

To set up IIS, we have to perform operation on two different Server.

Let us take one example:

We have tow server, one is our web server and one is our file server.
Ex: FILESERVER and WEBSERVER
we are storing files on FILESERVER And we host our application on WEBSERVER on IIS
Now First of all, we have set the IUSR_MACHINENAME user password

Here are the steps:

Go to control panel > Administrator Tools > Computer Management > In Computer management Expand the Tree " Local users and Groups And click on Users, so you can see list of users at right side panel,

From the right side list of users, select the IUSR_MACHINENAME AND Right click and click on Set Password….

So you can see Window like this:

Screenshot - Change_IUSR_Pwd.jpg

So one popup will arrive like this

Screenshot - SetPwd.jpg

so now click on Proceed

Screenshot - Enter_Pwd.jpg

Now set the Password for IUSR_MACHINENAME user. Ex: IUSR_WEBSERVER machine

Now open the IIS, And Right click on Your Application's Virtual Directory. Ex: "XYZ" Directory name. Click on Properties, so one popup will appear, now go to Directory Security Tab. And Under " Anonymous Access and Authentication Control " click on Edit Button

So you can see popup like this

Screenshot - Authentication_Methods.jpg

Now just Uncheck the "Allows IIS to Control Password" Check box and type the password, that you have set in Previous steps. For user IUSR_WEBSERVER

click Ok and apply settings.

Configure File Server.

Follow these steps
First you have to create new IUSR user with the same name as you have on WEBSERVER
so create new User on FILSERVER, with the IUSR_WEBSERVER name.
For that, follow these Steps,


Open Control panel > Administrator Tools > Computer Management > Click on Local Users and Groups

Select the Users folder > so list of existing users will appear.
Right click on Blank Space, and click on "New User"

Screenshot - New_User.jpg

Enter same user name that you have on WEBSERVER. Ex: IUSR_WEBSERVER

And also enter same password, that you have set for User "IUSR_WEBSERVER" for web server PC.

Un Check the "User must change password at next logon" check box and Checked on "Password Never Expired"

So now in Both Server(File and Web), Same User exists with Same Password.

Now locate the Directory, where you want to store or Retrieve Files, for Network Users from File Server
Ex: D:\TempFolder\list of files
So share the "TempFolder" and set Permission for IUSR_WEBSERVER User.

If IUSR_WEBSERVER user is not exists in "Group and User names", then add and set appropriate permission as per requirement.

So now we have IWEBSERVER user created on Fileserver and also we set the permission to network folder.

Enable impersonation in an ASP.NET

We can implement Impersonation using Different ways as per our Requirements.

To enable Impersonation in asp.net, you must include an <identity> tag in the Web.config file of this application and set the impersonate attribute to true.

<p><system.web> <br /><identity impersonate="true"/> <br /></system.web> </p>

but the above syntax is only for "IIS Authenticated Account or User"

if we want to implement impersonate for Specific user for all the Request then type the following in web.config file

<p><system.web> <br /><identity impersonate="true" userName="IUSR_WEBSERVER" password="password" /> </system.web></p>


in our case, we are using Specific user for all request so we are entering username as a "IUSR_WEBSERVER" and password

we can also implement Impersonate using Code, but I am not describing this here, you can check the Full Details about "How to Implement Impersonate through code" by click on this link http://support.microsoft.com/kb/306158

To access network file we can not map network drive and use it, but instead of map network drive, we have to use UNC (Universal Naming Convention)
So use like this:"\\Fileserver\FolderName\Files\File1.txt"
Instead of z:\Folder\Files\file1.txt

Here is code to retrieve network file and display on web page.

<p>Dim strPath As String = <a href="file://FILESERVER/TempFolder/FileName.pdf">\\FILESERVER\TempFolder\FileName.pdf</a> <br />If FileIO.FileSystem.FileExists(Trim(strPath)) Then <br />Response.WriteFile((strappath)) <br />Else <br />'Show message " File not found" <br />End If </p>

Conclusion

<p>So by using this article we can access network files and folder, there are also other way to implement this, but this is depend on users choice.<br />Hope this article will help you.... In case of any Problem or Issues , please feel free to post message.</p>

<p />

<p />

<p />

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Team Leader
India India
Jatin is Working in .Net Technology Since 2006. He has Completed Master of Science Degree in Information Technology. He Likes to learn Cutting edge technologies. He has good Skills in Asp.net, Vb.net,C#.Net, Crystal Reports,GDI+, Ajax, WCF, Silverlight SQL Server,IIS Admin,TFA ,Application Architecture Designing.

Comments and Discussions

 
QuestionHello Jatin Pin
jitendra prajapat4-Aug-15 0:45
jitendra prajapat4-Aug-15 0:45 
QuestionHow to store image & file folder in network drive? Pin
Member 112211859-Feb-15 23:49
Member 112211859-Feb-15 23:49 
Questionthanks Pin
Ronel Gonzales9-Oct-14 21:50
Ronel Gonzales9-Oct-14 21:50 
QuestionProblem in opening a folder in server from aspx page Pin
Jyoti_Shree8-Jan-13 22:03
Jyoti_Shree8-Jan-13 22:03 
GeneralMy vote of 5 Pin
vijaydanielj18-Apr-11 23:06
vijaydanielj18-Apr-11 23:06 
GeneralMy vote of 5 Pin
Member 775786314-Apr-11 23:15
Member 775786314-Apr-11 23:15 
GeneralMy vote of 4 Pin
Stranger_than_Fiction16-Sep-10 15:22
Stranger_than_Fiction16-Sep-10 15:22 
GeneralMy vote of 5 Pin
Member 421615823-Jul-10 9:51
Member 421615823-Jul-10 9:51 
QuestionHow to Access Network Files using asp.net Pin
viveksisodia11-Jul-10 23:49
viveksisodia11-Jul-10 23:49 
GeneralSolution won't work Pin
ChuckBorris23-Mar-09 23:28
ChuckBorris23-Mar-09 23:28 
GeneralSolutions from Microsoft Pin
Warren Reinke18-Sep-08 17:48
Warren Reinke18-Sep-08 17:48 
GeneralHai Jatin Pin
dardnaho18-Mar-08 20:33
dardnaho18-Mar-08 20:33 
GeneralCreates A gaping big security hole Pin
GaryWoodfine 7-Nov-07 0:18
professionalGaryWoodfine 7-Nov-07 0:18 
GeneralOther solutions to access network resources Pin
Jatin Prajapati9-Oct-07 1:40
Jatin Prajapati9-Oct-07 1:40 
Generali would rather use a dedicated domain/remote login Pin
luzer3-Aug-07 5:48
luzer3-Aug-07 5:48 
GeneralI hope No one actually does this Pin
thund3rstruck1-Aug-07 13:30
thund3rstruck1-Aug-07 13:30 
GeneralRe: I hope No one actually does this Pin
Jatin Prajapati1-Aug-07 21:20
Jatin Prajapati1-Aug-07 21:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.