1

我有一个在公司 Intranet 上运行的 ASP.NET MVC 3 网站,我想向该网站添加一个可以将文件上传到服务器的页面。

我正在使用 IIS 8 在 Windows Server 2012 上运行该站点。

IIS 配置:

应用程序池属性:

  • .Net框架v4.0
  • 托管管道模式:集成
  • 身份:本地系统

在站点节点下,认证设置为:

  • Windows 身份验证:启用(用户必须使用其公司 Windows 帐户登录)
  • 所有其他身份验证(包括匿名身份验证)设置为已禁用。

视窗权限:

Content目录中,我已授予系统、管理员和我的用户帐户的所有访问权限。

MVC 代码:

处理文件上传的 MVC Controller 方法包​​含以下代码:

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
    if (file != null && file.ContentLength > 0)
    {
        var fileName = Path.GetFileName(file.FileName);
        DateTime timestamp = DateTime.Today;
        var path = Path.Combine(Server.MapPath("~/Content/uploads"), fileName);
        if( !Directory.Exists(path))
            Directory.CreateDirectory(path);
        file.SaveAs(path);
    }

    return RedirectToAction("Index");
}

当我尝试使用上述控制器上传文件时,出现以下错误:

Server Error in '/' Application.

Access to the path 'C:\Sites\ClosedBeta\Content\uploads\test.csv' 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:\Sites\ClosedBeta\Content\uploads\test.csv' 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: 
Line 34:                 if( !Directory.Exists(path))
Line 35:                     Directory.CreateDirectory(path);
Line 36:                 file.SaveAs(path);
Line 37:             }
Line 38: 

Source File:  C:\Users\****\Documents\Visual Studio 2010\Projects\Solution\Project\Controllers\UploadTestController.cs    Line:  36 

Stack Trace: 

[UnauthorizedAccessException: Access to the path 'C:\Sites\ClosedBeta\Content\uploads\test.csv' is denied.]
  System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +10760710
  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) +1352
  System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +60
  System.IO.FileStream..ctor(String path, FileMode mode) +55
  System.Web.HttpPostedFile.SaveAs(String filename) +94
  System.Web.HttpPostedFileWrapper.SaveAs(String filename) +9
  Project.Controllers.UploadTestController.Upload(HttpPostedFileBase file) in C:\Users\****\Documents\Visual Studio 2010\Projects\Solution\Project\Controllers\UploadTestController.cs:36
  lambda_method(Closure , ControllerBase , Object[] ) +180
  System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
  System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +214
  System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
  System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
  System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +253
  System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +21
  System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
  System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +324
  System.Web.Mvc.Controller.ExecuteCore() +106
  System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +91
  System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
  System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +34
  System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +19
  System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
  System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
  System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +48
  System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
  System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
  System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
  System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629708
  System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

任何建议都将不胜感激

4

2 回答 2

0

我认为问题只是我需要等待一段时间才能使权限生效。

第二天回到相同的代码后,它工作得很好。

于 2013-07-10T11:51:17.067 回答
0

只需通过 iis 检查 IISUSER 并且 IUSR_ 具有读​​写权限

通常 plesk panel 和 cpanel 添加默认权限来读取列表和写入,但在某些情况下出现问题,您需要设置正确的权限。

所以你有两种方法来实现你的目标:

如果您可以访问 iss,请自行完成

或者

要求您的托管公司设置对该特定文件夹的写入和读取权限。

于 2014-04-06T11:17:55.467 回答