2015-10-13 70 views
1

在windows 2012服務器上託管的asp.net mvc 5網站中,我在網站文件夾結構(/Files/1.jpg等)內的一個文件夾中有一些靜態jpg文件。 )我希望一個API端點(GetPicture)能夠提供服務。允許asp.net mvc api調用訪問文件

api對Authentication標頭中的令牌使用基本認證。

在我的開發機器(vs2013,IIS Express)上,api服務jpg文件就好了。

但是,當我在測試/生產服務器上調用api endpoint(api/v1/GetPicture/5)時,我得到一個500 http代碼(UnauthorizedAccessException)並顯示錯誤消息:「Access to the path xxx被拒絕」。

用戶正在api調用中進行身份驗證。

API調用:

GET https://app.domain.com/api/v1/GetPicture/5 HTTP/1.1 
Host: app.domain.com 
Content-Type: application/json; charset=utf-8 
Content-Length: 49 
Authorization: Token 506b38df-947e-45a8-b2a0-49aa01e9a6f9 

的API終點:

[Route("GetPicture/{id:int}")] 
[HttpGet] 
public HttpResponseMessage GetPicture() 
{ 
    ... 
    ... 
    var result = new HttpResponseMessage(HttpStatusCode.OK); 
    var stream = new FileStream(path, FileMode.Open); 
    result.Content = new StreamContent(stream); 
    result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg"); 
    result.Content.Headers.ContentDisposition = 
     new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") 
     { 
      FileName = file.FileName 
     }; 
    return result; 
    ... 
    ... 
} 

我猜: - 這是一個IIS或文件系統的安全設置問題或 - 即圖片應該移動到App_Data - ?

回答

0

是的。 IIS應用程序池標識無法訪問硬盤上的任何位置。因此您可以將數據移動到您的c:\ inetpub下,或者授予訪問該應用程序池標識權限以訪問該目錄的權限。以下是關於應用程序池標識的鏈接http://www.iis.net/learn/manage/configuring-security/application-pool-identities

+0

/Files /文件夾是網站文件夾結構的一部分,因此應用程序池標識應該已經可以訪問文件夾中的文件 – mortenma71

相關問題