2014-11-06 90 views
5

我的應用程序使用ASP.NET和C#創建,在讀取文檔時使用臨時路徑存儲文檔,然後在刪除文檔後刪除它:訪問臨時文件夾時,ASP.NET未被授權訪問請求的資源

string path = string.Concat((Server.MapPath("~/temp/" + FileUpload1.FileName))); 

Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))), File.Delete); 

謝謝任何​​幫助。 StockControl文檔是一個示例,可以從網絡上的多臺計算機導入。基於錯誤信息,它看起來像我的文件不被上傳並保存到臨時目錄...

調試時

本地路徑(工程):

C:\Users\USER\Documents\Visual Studio 2013\Projects\WebApplication5\WebApplication5\temp 

的應用程序成功部署。然而,當我附加文件,然後按「導入」,我會顯示一條錯誤消息: 訪問路徑'C:\ inetpub \ wwwroot \ StockControl \ temp \ Book1.xls'被拒絕。

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:\inetpub\wwwroot\StockControl\temp\Book1.xls' 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 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:

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.

+1

做什麼錯誤消息說:添加到您的web.config – Zilog 2014-11-06 15:34:26

+1

錯誤說,訪問路徑被拒絕。它不可能比這更明顯:)確保您的應用程序池標識可以訪問應用程序的整個根文件夾。 – PoweredByOrange 2014-11-06 15:49:15

回答

5

一個非常簡單的修復,解決我的問題,在IIS我不得不右鍵點擊應用程序池,並設置.NET框架V4到集成,而不是經典/

另一種解決方案是設置正在被訪問以讀取/寫入需要它的用戶的特定文件夾,這可以由唯一用戶或在一個組織內,一組用戶完成

1

我有同樣的問題。

作出以下更改以解決問題。

(1)我已經去過wwwroot文件夾。右鍵單擊和安全選項卡。提供IISUSER設置wwwroot文件夾的讀寫權限。

(2)將應用程序池更改爲獨立池,並將身份設置爲應用程序池標識。