How to get cookies with angularjs from the server's http response

1

I wanted to consult the following, I have an application that uses CORS, and I need to obtain the cookie that the ws of the web api return to me. Observing in the browser I see that the cookie goes in the header as Set-Cookie. I already probe with angular ngCookies and it does not work. On the server I use .net with c # and CookieAuthentication from Owin.

Any other option?

this._$http.post('http://webapi:50830'+Utiles.prefijoApi(this._subscriptor) + 'cuenta/login', JSON.stringify(dto))
       .then(function (resultado) {
       var a = this._$cookies.get('Token');
           return resultado.data;
       }.bind(this), function (error) {
           return this._$q.reject(error.data);
       }.bind(this));

EDITING

Resolved the problem with the $ httpProvider.defaults.withCredentials = true; and it works correctly for requests to the WebApi. Now, I have the same problem with requests to MVC controllers, and I can not find a way to solve it, any ideas ??

    
asked by alejandro 07.11.2016 в 23:11
source

3 answers

0

you can include ngCookies , you add the reference to the file .js

<script src="js/angular-cookies.js">

And also to your module ['ngCookies'] and finally in your controller you do the injection $cookies

$cookies.get('Token');
    
answered by 07.11.2016 в 23:17
0

According to the angularJS documentation you can get the headers of the answer.

link $ http

You only have to indicate it to the function that you execute inside the "then":

this._$http.post('http://webapi:50830'+Utiles.prefijoApi(this._subscriptor) + 'cuenta/login', JSON.stringify(dto))
       .then(function (resultado, status, headers) {
       var a = headers['Set-Cookie'];

           return resultado.data;
       }.bind(this), function (error) {
           return this._$q.reject(error.data);
       }.bind(this));

You can also make a console.log (headers) to make sure that the content you are looking for is there.

Greetings.

    
answered by 08.11.2016 в 16:34
0

If you already have access to the response headers in your angular app, another way to keep your Token inside the app is with:

  

$ window.localStorage

You create a service (factory) and there you declare your setToken and getToken.

angular.module('app').factory('nombreDelServicio', ['$window', nombreDelServicio]);

function nombreDelServicio($window) {
    var storage = $window.localStorage;
    var cachedToken;
    setToken: function(token) {
      cachedToken = token;
      storage.setItem('token', angular.toJson(token));
    },
    getToken: function() {
      if(!cachedToken)
        cachedToken = storage.getItem('token');

      return cachedToken;
    }
}

You inject your service to the controller and when you receive the headers of the web api you assign the token in this way:

nombreDelServicio.setToken(response.headers('nombre-de-tu-header'));

When you want to read your token like this:

var token = nombreDelServicio.getToken();

There is also a module that to occupy the window local storage is called: angular-local-storage

    
answered by 09.11.2016 в 05:55