Access to the path is denied -How to solve this problem in ASP.NET

Posted Jun 16, 2009 by rajeshchoudhary / comments 0 comments / Print / Font Size Decrease font size Increase font size

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 and definetely annoying .this article is also about use of identity tag in web.config file .

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

        Dim path As String = "C:\AAQMS\AAQMS.html"

        Dim dt As DateTime

        dt = DateTime.Now

        Dim dat As String

        dat = dt.Day & "-" & dt.Month & "-" & dt.Year & "-" & dt.Hour

        'Response.Write(dat)

        Dim path2 As String = "C:\AAQMS1\AAQMS " & 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

 We can make our application run under another account than ASPNET in cases where.

We need access to the filesystem somewhere ASPNET doesn't have access. To

do this, we can insert the element in web.config like this:

  if we simply write this line in web.config then it will run under IUSR_MACHINENAME account.

Happy Coding.

Rate this Article:

Be the first to rate me.

  • Nothing Found!

    Why not submit your own content? Signup here.


* You must be logged in order to leave comments, please login or join us.

Comments

No comments yet.



Bookmark and Share
Sign up for our email newsletter
Name:
Email: