solution to Access to the path is denied asp.net

System.UnauthorizedAccessException: Access to the path is denied. This problem occurs when you are going to use file system from an ASP.NET web application.

I designed an asp.net page to move a file from one folder to another as a part of my web application. Code written for page load event was like this

Dim path As String = "C:AAQMSAAQMS.html"
Dim dt As DateTime
dt = DateTime.Now
Dim dat As String
dat = dt.Day & "-" & dt.Month & "-" & dt.Year & "-" & dt.Hour
Dim path2 As String = "C:AAQMS1AAQMS " & dat & ".html"
If File.Exists(path) = True Then
File.Move(path, path2)
Else
Response.Write("File doesn't Exist")
End If

If File.Exists(path) = True Then
File.Delete(path)
End If

I wanted to move file aaqms.html from C:AAQMS to C:AAQMS1 .This was running on my XP machine without any problem but when i transferred this application to my windows 2003 server i was facing this problem

Server Error in '/AAQMS-SERVER' Application.
Access to the path is denied.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: Access to the path is denied.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

I managed to find a solution that by giving access right for these folders to ASPNET user it will work

I provided full rights to ASPNET user by going to properties of the folder. Even i assigned full rights to user IUSR_MACHINENAME(anonymous user) but it didn't work.

The solution which worked in my case is
By default windows account ASPNET is used to access asp.net web applications.ASPNET user account has limited access to resources compared to anonymous account IUSR_MACHINENAME. We can make our asp.net applications run under another account than ASPNET.If we want our application to run under windows account we have to impersonate identity by putting following lines in web.config file


if we simply write these lines in web.config then it will run under IUSR_MACHINENAME account.Even we can specify particular Windows account by putting UserName and password.

Your rating: None Average: 5 (2 votes)