Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Access Network Files using asp.net

0.00/5 (No votes)
1 Aug 2007 2  
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.

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

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

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


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.

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

Conclusion

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.
Hope this article will help you.... In case of any Problem or Issues , please feel free to post message.

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