2016-05-31 71 views
1

有一個網站和一個web api。從web api服務器接收的所有文件。在標頭中發送令牌GET方法

我在網站上有ADFS OAUTH2授權。 我需要從auth令牌獲取web api中的圖片。

所以,現在我做這樣的事情:

<img src='webApiUrl/Photo/Id?token=token_value' alt /> 

不過我有帶令牌長度的錯誤。對於一些客戶來說這是很長的時間,我無法控制它。 我可以用xhr請求發送授權頭,但我不明白如何爲通過src從html請求資源的站點設置授權頭。

你能幫我解決嗎?

回答

0

每當您向Web API發送HTTP請求時,都可以使用Angular攔截器將令牌放在請求標頭上。在這裏,我選擇演示承載認證。就像這樣:

appName.config(["$httpProvider", ($httpProvider: ng.IHttpProvider) => { 
       $httpProvider.interceptors.push(<any>["$q", "$location", 
        ($q: ng.IQService, $location: ng.ILocationService) => { 
         return { 

        // config is the request data, including all its properties 
          'request': (config) => { 

           // Intercepting only the API requests 
           if (config.url.indexOf(apiServerUrl) >= 0) { 

           // Getting the token from local storage for example 
            var token = localStorage.getItem("token"); 

           // Placing the token in the right header           
            if (token) 
            config.headers["Authorization"] = "Bearer " + token; 
           } 
           return config; 
          } 
         } 
        } 
       ]); 
      }]); 
+0

謝謝,但我嘗試這種方法,攔截器不處理瀏覽器的請求只$ $。 –

+0

我已更新問題 –

-1

也許這將解決您的問題:http://blog.jsgoupil.com/request-image-files-with-angular-2-and-an-bearer-access-token/

它涉及註冊管,這樣就可以使用安全src屬性與img標籤。

+0

歡迎您提供解決方案的鏈接,但請確保您的答案在沒有它的情況下有用:[添加鏈接的上下文](// meta.stackexchange.com/a/8259),以便您的同伴用戶有一些想法是什麼以及它爲什麼在那裏,然後引用你鏈接到的頁面中最相關的部分,以防目標頁面不可用。 [僅僅是一個鏈接的答案可能會被刪除。](// stackoverflow.com/help/deleted-answers) – FelixSFD

+0

謝謝你,我相應地編輯了我的回覆。 – Sammy