asp.net web api - AngularJS passing Response as header/Web API -


new angular learning. goal is: have login view passes user object our api, api checks against user database , reposnds base64 conversion of username , password. have setup page returns simple json if user passes authorization : base ... header receive data. if not responds 401 , set global 401 handler route "/login".

my issue in loginservice receive base64 key , can alert out show received it. "undefined" if inject service json page header so..

$http.get('http://myurl/api/project', { headers: {"authorization": "basic " +    userloginservice.mydata}}) 

so basic thing trying pass data 1 service service, have seen how 1 controller but, not sure here. sample code great. lot in advance.

i'm not sure can share data 1 service service without passing data parameter method or constructor. e.g.

//inside of service this.makeapicall = function(token){     $http.get('http://...', {headers: {'authorization': 'basic ' + token}}); } 

but want store authentication key future http requests anyway , best store key in way wouldn't require ask authentication each time need make request. suggest store token in browser's localstorage or cookie can retrieve token through term user logged in.



as side note, if find you're going have pass authorization header each time make api call, may want configuring $httpprovider on module (this example assumes using localstorange , have authkey stored in localstorage:

angular.module('myapp', []).run(function($http, localstoragefactory){     $http.defaults.headers.common['authorization'] = 'basic ' + localstoragefactory.get('authkey'); }); 

Comments