3

I am implementing authentication where I added a token in response from server side. enter image description here

I am trying to read this header value returned from server in angularjs however I don't see this header value present. Here is my javascript console. enter image description here

EDIT:

return base.SendAsync(request, cancellationToken).ContinueWith(task =>
            {
                var response = task.Result;
                if (response.RequestMessage.Headers.Contains(TOKEN_NAME))
                {
                    string token = response.RequestMessage.Headers.GetValues(TOKEN_NAME).FirstOrDefault();
                    response.Headers.Add("Access-Control-Expose-Headers", TOKEN_NAME);
                    response.Headers.Add(TOKEN_NAME, token);
                }

                return response;
            });
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286

1 Answers1

4

Is the access/authenticate endpoint returning the token as data in the success method or is the token being set in the server side code?

-Update-

If you set the token in HttpContext.Current.Response.AppendHeader('X-Token', "<some token value">); You should be able to grab it in your $http promise

$http.get("<api endpoint>").then(function(data){
            $log.log("data.config", data.config);
            $log.log("data.headers()", data.headers());
            $log.log("X-Token Header", data.headers()['x-token']);
});

data.config is the headers sent to the server such as the accept and any authorization headers.

data.headers() is the function that returns all headers that were set server side. response.Headers.Add("Access-Control-Expose-Headers", TOKEN_NAME); this line will ensure this header is available to the server

enter image description here

enter image description here

data.config vs data.headers()

So if I understand correctly your passing x-token in the header of the api request and the Delegating Handler is looking for TOKEN_NAME and then resetting it and then your trying to access it in the promise of $http. I just put together a test for this case and im getting back x-token;

-Sample angular app

        (function () {
        var app = angular.module('app', []);

        app.config(function ($httpProvider) {
            $httpProvider.defaults.headers.common["x-token"] = "";
        });


        app.controller('home', function ($http, $log) {
            $http.get('/api/car/get')
                .then(function (response) {
                $log.log("Response headers",response.headers());
                $log.log(response.headers()["x-token"]);
            });
        });

    })();

-Console window enter image description here

-CustomDelegatingHandler i dont use the variable token because I dont have a token endpoint to get one. Instead I just passed back a random GUID.

 protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
          CancellationToken cancellationToken)
        {
            return await base.SendAsync(request, cancellationToken).ContinueWith(task =>
            {
                var response = task.Result;
                //Are you sure your passing this check by setting the x-token common header in your angular $http requests?
                if (response.RequestMessage.Headers.Contains(TOKEN_NAME))
                {
                    string token = response.RequestMessage.Headers.GetValues(TOKEN_NAME).FirstOrDefault();
                    response.Headers.Add("Access-Control-Expose-Headers", TOKEN_NAME);
                    response.Headers.Add(TOKEN_NAME, Guid.NewGuid().ToString());
                }

                return response;
            }, cancellationToken);
        }
jkerb
  • 179
  • 5
  • Token is being returned in header and not data. I am not sure if sending token as data would be a good practice. Currently I am setting token in header on server side code. – Shantanu Gupta Nov 22 '15 at 20:14
  • I am not getting HttpContext into my DelegatingHandler class – Shantanu Gupta Nov 22 '15 at 20:39
  • how are you setting the header then? – jkerb Nov 22 '15 at 21:11
  • Just highlighting your key take away from your answer to reflect what I was missing. Thanks for your help. It worked when I used data.headers(). This is not available in `$http.success(data, status)` – Shantanu Gupta Nov 23 '15 at 05:31
  • You can try it in the success method it would be $http.get (endpoint).success (function(response, staus, headers){ $log.log (headers ('x-token');}); I'm not 100% on this being correct as I have never actually tried it. – jkerb Nov 23 '15 at 11:40