Avatar of Peter Chan
Peter Chan
Flag for Hong Kong asked on

Problem with the path

Hi,
I don't know why I get this
Server Error in '/Login' Application.
--------------------------------------------------------------------------------


Access to the path 'C:\inetpub\VS2012\Xml\common.xml' 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 'C:\inetpub\VS2012\Xml\common.xml' 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 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, 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 File 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.

Source Error:


An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:



[UnauthorizedAccessException: Access to the path 'C:\inetpub\VS2012\Xml\common.xml' is denied.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +216
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1430
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +208
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +87
   System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding) +63
   Edit_User._Default.LoginButton_Click(Object sender, EventArgs e) +1118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18034

running the VS 2012 project in Win 2008 server, while I see the current domain administrator is having Full control on this path
C:\inetpub\VS2012\Xml
ASP.NETMicrosoft ApplicationsMicrosoft Development

Avatar of undefined
Last Comment
guru_sami

8/22/2022 - Mon
Dale Burrell

The error tells your everything "ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity".

Grant the ASP.NET process identity rights to that folder and problem solved.
Peter Chan

ASKER
Sorry, where to do the grant?
Dale Burrell

On the folder that you want the asp.net site to write to, I would recommend limiting it to the single folder you need, and ideally put it outside of the site as if its inside the site it becomes a security issue.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Peter Chan

ASKER
Sorry, I change it to directly refer to the file like
C:/inetpub/wwwroot/Xml/common.xml

but I've got this.
Server Error in '/Login' Application.
--------------------------------------------------------------------------------


'C:/inetpub/wwwroot/Xml/common.xml' is a physical path, but a virtual path was expected.
  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.Web.HttpException: 'C:/inetpub/wwwroot/Xml/common.xml' is a physical path, but a virtual path was expected.

Source Error:


 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:



[HttpException (0x80004005): 'C:/inetpub/wwwroot/Xml/common.xml' is a physical path, but a virtual path was expected.]
   System.Web.Util.UrlPath.CheckValidVirtualPath(String path) +12601025
   System.Web.Util.UrlPath.Combine(String appPath, String basepath, String relative) +140
   System.Web.VirtualPath.Combine(VirtualPath relativePath) +222
   System.Web.HttpRequest.MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, Boolean allowCrossAppMapping) +127
   System.Web.HttpServerUtility.MapPath(String path) +252
   Edit_User._Default.LoginButton_Click(Object sender, EventArgs e) +1086
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18034  

How to resolve it?
Peter Chan

ASKER
Any help to this?
Dale Burrell

You can't be granting rights to the correct user then, you need to be sure its the user that is associated with you site app pool.

http://www.iis.net/learn/manage/configuring-security/application-pool-identities
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
guru_sami

So your authorization error is gone correct? And you have the second error?
Can you show the code where you are trying to access that file?
May be some path mapping issue...
Peter Chan

ASKER
Thanks a lot to all.

Sami,
Here is how I refer to the file in the project
string FILENAME = Server.MapPath(@"C:\inetpub\wwwroot\Xml\common.xml");
...
guru_sami

What is the XML in the path? a folder in a site or a site's root folder?
Where is your app?
As the error says, Server.Map needs a virtual or relative path e.g.
Server.MapPath("~/xml/common.xml");
But you are referring the physical path.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Peter Chan

ASKER
Thanks a lot.
I now have such line below

string FILENAME = Server.MapPath("~/xml/common.xml");
..

and "xml" is in the same level like Login project folder, like

C:\inetpub\VS2012\Xml
C:\inetpub\VS2012\Login

I still have got this. How to resolve it?

Server Error in '/Login' Application.
--------------------------------------------------------------------------------


Could not find a part of the path 'C:\inetpub\VS2012\Login\xml\common.xml'.
  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.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\inetpub\VS2012\Login\xml\common.xml'.

Source Error:


 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:



[DirectoryNotFoundException: Could not find a part of the path 'C:\inetpub\VS2012\Login\xml\common.xml'.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +338
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1430
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +208
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +87
   System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding) +63
   Edit_User._Default.LoginButton_Click(Object sender, EventArgs e) +1118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18034
ASKER CERTIFIED SOLUTION
Dale Burrell

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Peter Chan

ASKER
Sorry, with this line
string FILENAME = Server.MapPath("~") + "..\\xml\\common.xml";

I've still got this, when running the newly deployed project.
Server Error in '/Login' Application.
--------------------------------------------------------------------------------


Could not find a part of the path 'C:\inetpub\VS2012\Login\xml\common.xml'.
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.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\inetpub\VS2012\Login\xml\common.xml'.

Source Error:


An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:



[DirectoryNotFoundException: Could not find a part of the path 'C:\inetpub\VS2012\Login\xml\common.xml'.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +338
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1430
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +208
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +87
   System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding) +63
   Edit_User._Default.LoginButton_Click(Object sender, EventArgs e) +1123
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18034
Dale Burrell

Dude, you need to debug it - I've tried to explain the principles behind what is happening/required but I can't get it perfect remotely. Step through the code, find out what is happening at that point, and correct so it returns the right result. As you have the issue right in front of you that will be the quickest option.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Peter Chan

ASKER
How can I point to "xml" folder that is in the same level of the project folder, being run?
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Peter Chan

ASKER
With this line
string FILENAME = Server.MapPath("~") + "..\\..\\xml\\common.xml";

I still have got this error. Why?
Server Error in '/Login' Application.
--------------------------------------------------------------------------------


Access to the path 'C:\inetpub\VS2012\xml\common.xml' 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 'C:\inetpub\VS2012\xml\common.xml' 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 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, 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 File 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.

Source Error:


 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:



[UnauthorizedAccessException: Access to the path 'C:\inetpub\VS2012\xml\common.xml' is denied.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +216
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1430
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +208
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +87
   System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding) +63
   Edit_User._Default.LoginButton_Click(Object sender, EventArgs e) +1123
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18034
Dale Burrell

Its a permission error again, you need to grant access to the XML folder to the identity that that site is running under. Most likely reason its still happening is you're not sure which identity the site is running under.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
guru_sami