读取文件时出错

问题描述:

当我点击MVC页面上的链接时,我无法打开文件。我收到以下消息。我在代码中添加了模拟。我可以删除并保存文件。读取文件时出错

Access to the path '\servername\folder1\folder2\folder3\foder4\filename.pdf' 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 '......same as above....' 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 <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 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:

Line 35:   ' This gets executed for every action on this controller 
Line 36:   ViewData("READONLY") = IIf(SessionData.IsReadOnly, "Y", "N") 
Line 37:   MyBase.Execute(requestContext) 
Line 38:  End Sub 
Line 39: 

Source File: C:\Posfx\trunk\posfx\camonline\Controllers\ApplicationController.vb Line: 37

Stack Trace:

[UnauthorizedAccessException: Access to the path '\\cambridge-uat-d\NetworkShare\DealDocumentShare\201102\online\136-test file 2.pdf' is denied.] 
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +7716623 
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) +1162 
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +66 
System.Web.HttpResponse.TransmitFile(String filename, Int64 offset, Int64 length) +134 
System.Web.HttpResponse.TransmitFile(String filename) +12 
System.Web.HttpResponseWrapper.TransmitFile(String filename) +11 
System.Web.Mvc.FilePathResult.WriteFile(HttpResponseBase response) +37 
System.Web.Mvc.FileResult.ExecuteResult(ControllerContext context) +268 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +10 
System.Web.Mvc.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e() +20 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +251 
System.Web.Mvc.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10() +19 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +251 
System.Web.Mvc.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10() +19 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +251 
System.Web.Mvc.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10() +19 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +178 
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399 
System.Web.Mvc.Controller.ExecuteCore() +126 
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27 
Cambridge.Online.ApplicationController.Execute(RequestContext requestContext) in C:\Posfx\trunk\posfx\camonline\Controllers\ApplicationController.vb:37 
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7 
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151 
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +57 
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7 
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181 
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75 
+0

任何最终的解决方案与完整的源代码示例工作呢? – Kiquenet 2013-07-11 06:01:32

+0

很久以前。我记错了。但是,这是IIS应用程序池 – 2013-07-11 13:36:11

我认为错误信息比清楚,它甚至告诉你该怎么做,这是罕见的。您的网站在特权帐户下运行。根据您使用的IIS版本,此帐户可能会有所不同。因此,您应考虑授予对此帐户的读取访问权限给您尝试访问的远程文件夹,根据您获得的例外情况,该帐户是\servername\folder1\folder2\folder3\foder4

您在说您已添加模拟,但模拟仅适用于本地资源。在你的情况下,似乎你正试图访问远程服务器(远程共享)。在这种情况下,您需要授权而不是冒充。因此,您可以将您的网站配置为在可以读取此远程共享的域帐户下运行。