最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to read Token from header of a response in angularjs - Stack Overflow

programmeradmin2浏览0评论

I am implementing authentication where I added a token in response from server side.

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.

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;
            });

I am implementing authentication where I added a token in response from server side.

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.

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;
            });
Share Improve this question edited Nov 22, 2015 at 21:40 Shantanu Gupta asked Nov 22, 2015 at 19:45 Shantanu GuptaShantanu Gupta 21.1k56 gold badges186 silver badges293 bronze badges 2
  • 1 Possible answer here – Dvir Commented Nov 22, 2015 at 20:03
  • @Dvir: You ment added equally to reach out to working solution. – Shantanu Gupta Commented Nov 23, 2015 at 5:40
Add a ment  | 

1 Answer 1

Reset to default 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

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.mon["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

-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 mon 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);
        }
发布评论

评论列表(0)

  1. 暂无评论