javascript 如何从 angularjs 的响应头中读取令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33859385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read Token from header of a response in angularjs
提问by Shantanu Gupta
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.
我试图在 angularjs 中读取从服务器返回的这个标头值,但是我没有看到这个标头值存在。这是我的 javascript 控制台。
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;
});
回答by jkerb
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
如果您将令牌设置为HttpContext.Current.Response.AppendHeader('X-Token', "<some token value">);
您应该能够在您的 $http 承诺中获取它
$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.config 是发送到服务器的标头,例如接受和任何授权标头。
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
data.headers()
是返回在服务器端设置的所有标头的函数。
response.Headers.Add("Access-Control-Expose-Headers", TOKEN_NAME);
此行将确保此标头可用于服务器
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;
因此,如果我理解正确,您在 api 请求的标头中传递的 x-token 和委托处理程序正在寻找 TOKEN_NAME,然后重置它,然后您尝试在 $http 的承诺中访问它。我刚刚为这个案例做了一个测试,我取回了 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"]);
});
});
})();
-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.
-CustomDelegatingHandler 我不使用变量令牌,因为我没有令牌端点来获取它。相反,我只是传回了一个随机的 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);
}