I / O permissions for medium trust

According to this MSDN article on medium trust under medium trust:

FileIOPermission is limited. This means that you can only access files in the application hierarchy virtual directory. Your application is provided. Read, write, add and determine the resolution path for your application virtual hierarchy directory.

However, for my current hosting provider, applications work under average trust, and when I try to read / write a file to the root folder of the application, I get an error access to path 'myfile.xml' denied.

This file is read using the following bit of code

XElement file = XElement.Load(HttpContext.Current.Server.MapPath("~/myfile.xml"));

Update full error:

Access to the path 'C: \ WebSites \ mywebsite \ myfile.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 occurred in the code.

Exception Details: System.UnauthorizedAccessException: Access to path 'C: \ WebSites \ mywebsite \ myfile.xml' is denied.

ASP.NET does not have permission to access the requested resource. Consider granting access rights to a resource to an ASP.NET request identifier. ASP.NET has a basic process identifier (usually {MACHINE} \ ASPNET on IIS 5 or network service on IIS 6 and IIS 7, and a configured application pool identifier in IIS 7.5), which is used if the application does not impersonate. If the application pretends to be using, the identity will be an anonymous user (usually IUSR_MACHINENAME) or an authenticated user request.

ASP.NET , , "" "". "", . ASP.NET, .

:

-. .

:

[UnauthorizedAccessException: 'C:\WebSites\mywebsite\myfile.xml' .]
System.IO.__ Error.WinIOError(Int32 errorCode, String maybeFullPath) +12892935 System.IO.FileStream.Init(String path, FileMode, FileAccess, Int32, Boolean useRights, FileShare, Int32 bufferSize, FileOptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) +2481
System.IO.FileStream..ctor(String , FileMode, FileAccess , FileShare, Int32 bufferSize, FileOptions, String msgPath, Boolean bFromProxy) +229 System.IO.FileStream..ctor(String , FileMode, FileAccess , FileShare) +102
System.Xml.XmlWriterSettings.CreateWriter(String outputFileName) +5224496
System.Xml.Linq.XElement.Save(String fileName, SaveOptions) +108
mesoBoard.Services.SiteConfig.UpdateCache() +1971 mesoBoard.Web.MvcApplication.OnApplicationStarted() +62 Ninject.Web.Mvc.NinjectHttpApplication.Application_Start() +604

[HttpException (0x80004005): 'C:\WebSites\mywebsite\myfile.xml' .]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext , HttpApplication) +3985477 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext, MethodInfo []) +191
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo [] , IntPtr , HttpContext) +325 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr , HttpContext) +407 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +375

[HttpException (0x80004005): 'C:\WebSites\mywebsite\myfile.xml' .]
System.Web.HttpRuntime.FirstRequestInit(HttpContext ) +11524352
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext ) +141
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext) +4782309

+5
5

, , -, / /. , , , . , wwwroot -, .

webdir/
webdir/data/ myfile.xml

webdir/Wwwroot
webdir/Wwwroot/default.aspx

+1

- . , , -, IIS, , .

, xml . "~/App_Data/", .NET, , - www.yoursite.com/data/myfile.xml , App_Data .

App_Data, Visual Studio?

0

XElement. "System.Xml.Linq", , " " . Linq , procedure

0

Medium Trust , $AppDir. , FileIOPermission, , $AppDir. , , Server.MapPath( "/" ) " " System.Security.Permissions.FileIOPermission, mscorlib, Version = 4.0.0.0, = , PublicKeyToken = b77a5c561934e089 " "

, $AppDir ". Server.MapPath() -.

0

I installed the site on GoDaddy and found that the only way to enable write access is to enable it for the entire web root , but it may just be a limitation for this hosting plan

-1
source

All Articles