2012-04-23 33 views
3

我嘗試從本地網絡中的共享文件夾訪問文件時遇到問題。
當我在Visual Studio中運行應用程序時,它工作正常,我可以下載該文件,但是當我將應用程序部署到IIS時,它根本不起作用。
我不明白爲什麼,因爲我給予所有用戶的完全權限,包括NETWORK SERVICE和IIS_USR。
我正在使用我的電腦進行測試。它具有Windows 7並運行IIS 7.5。我嘗試訪問的共享文件夾位於未安裝IIS的Windows Vista安裝中。從IIS中部署的應用程序訪問共享文件夾

下載文件的代碼是這樣的:

protected void button_Click(object sender, EventArgs e) 
{ 
    Response.Clear(); 
    Response.ContentType = "application/octet-stream"; 
    Response.AddHeader("Content-Disposition", "attachment; filename=S10.png"); 
    Response.WriteFile(@"\\192.168.1.82\Machine\file.png"); 
    Response.End(); 
} 

,它給我下面的錯誤:

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: Acesso negado ao caminho '\192.168.1.82\Machine\file.png'.

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.

請有人可以幫助我。 在此先感謝。

回答

1

你給了整個文件系統的訪問權限(完全控制是矯枉過正爲此,讀取或修改應該是罰款),但你可能會發現共享權限尚處於只讀默認。您需要更改共享權限。

Read this for more info

編輯:調試時,它的工作原理確定的原因是因爲它是使用你登錄所用,而不是在VS. ASPNET帳戶的帳戶

創建一個新的應用程序池,在服務帳戶下運行,該服務帳戶是IIS服務器上IIS_WPG組的成員,並且對您想要回寫的共享具有權限(請記住將服務帳戶設置爲密碼永不過期)。授予IIS_WPG組讀取&對網站目錄執行,列出文件夾內容和讀取權限。將Web應用程序更改爲在此新的Web池中運行。

+0

Thaks您的回覆,但it's不是那個意思,我檢查了共享權限和it's罰款,這也適用於Visual Studio中運行良好,但只有當我運行在IIS中的應用程序給我的問題。 – Luis 2012-04-23 19:46:41

+0

回覆編輯後發表評論。 – 2012-04-23 20:11:45

+0

感謝您的幫助。我已經嘗試過,並沒有工作,總是得到相同的錯誤,無法找出問題所在。 – Luis 2012-04-24 15:47:32

0

解決方案1:

您可以定義一個域用戶。讓IIS服務器與該用戶一起運行(通過池標識或「連接爲」)。授予此用戶對共享文件夾的訪問權限。

解決方案2:

添加IIS服務器帳戶到用戶組包含共享文件夾的服務器,提供正確的共享文件夾這個用戶訪問。

0

如果文件和機文件夾在C盤,那麼你還需要給權限包含該文件路徑的根文件夾,因爲C盤是通常的讀/寫保護,因爲它有系統文件在裏面。

因此,例如,如果該文件是在路徑C:\計算機\ file.png。 然後,您需要添加並將IIS用戶組的相關權限賦予機器文件夾,而不僅僅是文件,即file.png。

相關問題