加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 中等信任文件I / O权限

发布时间:2020-12-16 07:39:00 所属栏目:asp.Net 来源:网络整理
导读:根据这个 MSDN article about medium trust,在中等信任下: FileIOPermission is restricted. This means you can only access files in your application’s virtual directory hierarchy. Your application is granted Read,Write,Append,and PathDiscover
根据这个 MSDN article about medium trust,在中等信任下:

FileIOPermission is restricted. This
means you can only access files in
your application’s virtual directory
hierarchy. Your application is granted
Read,Write,Append,and PathDiscovery
permissions for your application’s
virtual directory hierarchy.

但是,对于我当前的托管服务提供商在中等信任下运行应用程序,当我尝试在应用程序的根文件夹中读取/写入文件时,我获得了对路径’myfile.xml’被拒绝错误的访问权限.

使用以下代码读取此文件

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

更新完整错误:

Access to the path
‘C:WebSitesmywebsitemyfile.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:WebSitesmywebsitemyfile.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,
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.

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:WebSitesmywebsitemyfile.xml’ is
denied.]
System.IO.__Error.WinIOError(Int32
errorCode,String maybeFullPath)
+12892935 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) +2481
System.IO.FileStream..ctor(String
path,FileMode mode,FileAccess
access,FileShare share,Int32
bufferSize,FileOptions options,
String msgPath,Boolean bFromProxy)
+229 System.IO.FileStream..ctor(String
path,FileShare share) +102
System.Xml.XmlWriterSettings.CreateWriter(String
outputFileName) +5224496
System.Xml.Linq.XElement.Save(String
fileName,SaveOptions options) +108
mesoBoard.Services.SiteConfig.UpdateCache()
+1971 mesoBoard.Web.MvcApplication.OnApplicationStarted()
+62 Ninject.Web.Mvc.NinjectHttpApplication.Application_Start()
+604

[HttpException (0x80004005): Access to
the path
‘C:WebSitesmywebsitemyfile.xml’ is
denied.]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext
context,HttpApplication app) +3985477
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr
appContext,HttpContext context,
MethodInfo[] handlers) +191
System.Web.HttpApplication.InitSpecial(HttpApplicationState
state,MethodInfo[] handlers,IntPtr
appContext,HttpContext context) +325
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr
appContext,HttpContext context) +407
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr
appContext) +375

[HttpException (0x80004005): Access to
the path
‘C:WebSitesmywebsitemyfile.xml’ is
denied.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext
context) +11524352
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext
context) +141
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest
wr,HttpContext context) +4782309

解决方法

您需要确保运行网站的应用程序池的用户帐户具有对文件/文件夹的读/写权限.默认情况下,我认为您应该具有读取权限但不具有写入权限.此外,出于安全原因,将该文件从wwwroot文件夹中移出到不会损坏整个应用程序的内容可能是个好主意.

webdir/data
webdir/data/myfile.xml

webdir/wwwroot webdir/wwwroot/default.aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读