2013-06-12 41 views
1

我有一個ASP.NET MVC 3網站運行在公司內部網上,我想添加一個頁面,可以上傳文件到服務器。授予ASP.NET MVC 3網站寫訪問〜/內容/上傳文件夾

我運行Windows Server 2012在網站上與IIS 8

IIS配置:

應用程序池的屬性:

  • .Net框架V4.0
  • 管理管道模式:集成
  • 標識:本地系統

下的站點節點,身份驗證設置爲:

  • Windows身份驗證:啓用(用戶必須使用他們的企業Windows帳戶登錄)
  • 其他所有身份驗證,包括匿名身份驗證設置爲禁用。

Windows權限:

Content目錄,我已授予的所有訪問權限系統,管理員和我的用戶帳戶。

MVC代碼:

的MVC控制器的方法來處理文件上傳包含以下代碼:

[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 

任何書於將apreciated

+0

您的應用程序是否在冒充或不? –

+0

@Garath目前沒有 – Chopo87

+0

您確定您的應用程序池位於本地系統而不是網絡用戶或AppPool用戶?以上問題意味着:您更改了默認用戶? –

回答

0

我認爲這個問題只是我需要稍微等待一下權限才能生效。

第二天回到相同的代碼後,它完美地工作。

0

簡單地去雖然IIS檢查IISUSER和IUSR_有權讀取和寫入

一般的Plesk面板的cPanel添加默認的權限讀取列表和寫入,但在某些情況下出現錯誤和u需要設置正確允許。

所以,你有2路才達到你的目標:

如果你有機會到國際空間站都由你自己做到這一點

問到你的託管公司設置寫入和讀取的權限在此特定文件夾。