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;
            });
2
  • 1
    Possible answer here Commented Nov 22, 2015 at 20:03
  • @Dvir: You comment added equally to reach out to working solution. Commented Nov 23, 2015 at 5:40

1 Answer 1

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);
        }
Sign up to request clarification or add additional context in comments.

5 Comments

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.
I am not getting HttpContext into my DelegatingHandler class
how are you setting the header then?
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)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.