Angularjs 在标头GET方法中发送令牌

Angularjs 在标头GET方法中发送令牌,angularjs,image,get,header,asp.net-web-api2,Angularjs,Image,Get,Header,Asp.net Web Api2,有一个站点和一个web api。我从web api服务器收到的所有文件 我在网站上有ADFS OAUTH2授权。 我需要使用auth令牌从web api获取图像 所以现在我做了这样的事情: <img src='webApiUrl/Photo/Id?token=token_value' alt /> 但是我有一个标记长度的bug。对于一些客户来说,这段时间很长,我无法控制。 我可以将授权标头与xhr请求一起发送,但我不知道如何为通过src从html请求资源的站点设置授权标头 你能

有一个站点和一个web api。我从web api服务器收到的所有文件

我在网站上有ADFS OAUTH2授权。 我需要使用auth令牌从web api获取图像

所以现在我做了这样的事情:

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

但是我有一个标记长度的bug。对于一些客户来说,这段时间很长,我无法控制。 我可以将授权标头与xhr请求一起发送,但我不知道如何为通过src从html请求资源的站点设置授权标头


你能帮我修复它吗?

每当你对Web API有HTTP请求时,你可以使用角度拦截器将你的令牌放在请求头上。这里我选择演示承载身份验证。像这样:

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;
                            }
                        }
                    }
                ]);
            }]);
appName.config([“$httpProvider”,($httpProvider:ng.IHttpProvider)=>{
$httpProvider.interceptors.push([“$q”,“$location”,
($q:ng.IQService,$location:ng.ILocationService)=>{
返回{
//config是请求数据,包括其所有属性
“请求”:(配置)=>{
//仅拦截API请求
if(config.url.indexOf(apiServerUrl)>=0){
//例如,从本地存储获取令牌
var token=localStorage.getItem(“token”);
//将令牌放置在右侧标头中
如果(令牌)
配置头[“授权”]=“承载”+令牌;
}
返回配置;
}
}
}
]);
}]);

也许这可以解决您的问题:


它包括注册一个管道,以便您可以将安全的
src
属性与
img
标记一起使用。

谢谢,但我尝试了这种方法,拦截器只处理浏览器的请求$http。欢迎链接到解决方案,但是请确保你的答案在没有它的情况下是有用的:这样你的其他用户就会知道它是什么以及它为什么在那里,然后引用你链接到的页面中最相关的部分,以防目标页面不可用。