I try to make authenticated request to a WebApi server, using angular.
This is how I make the request:
var config = {headers: {Authorization: "Bearer " + JSON.parse($cookies.token)}};
var url = 'http://localhost:53889/api/Values/5';
$http.get(url,config).success(function(response){
console.log(response);
})
And this is how I set the authentication:
login: function(credentials){
return $http({
method: 'POST',
url: url + '/Token',
headers: {'Content-Type' : 'application/x-www.form-urlencoded'},
transformRequest : function(obj){
var str = [];
for(var p in obj){
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
},
data: credentials
});
var success = function(data){
AuthorizationFactory.setCurrentUser(credentials.username);
ApiFactory.init(data["access_token"]);
$cookieStore.put('token', JSON.stringify(data["access_token"]));
};
The success method is called like this: AuthorizationFactory.login(credentials).success(success)
.
But everytime I try to make a authenticated request, I get a 401 error message.
The headers look like this:
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:ro-RO,ro;q=0.8,en-US;q=0.6,en;q=0.4
Authorization:Bearer "4N_9ScOCtFxTgNs....t"
Cache-Control:no-cache
Connection:keep-alive
Cookie:.AspNet.Cookies=Rx...y0OTHrvE
Host:localhost:53889
Pragma:no-cache
Referer:http://localhost:53889/app/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36
X-Access-Token:"\"4N_9ScO...XZ6t\""
What am I doing wrong? What I'm missing?