Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag 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
Avatar of Dale Burrell
Dale Burrell
Flag of New Zealand image

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.
Avatar of Peter Chan

ASKER

Sorry, where to do the grant?
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.
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?
Any help to this?
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
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...
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");
...
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.
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
Avatar of Dale Burrell
Dale Burrell
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
How can I point to "xml" folder that is in the same level of the project folder, being run?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.