gpt4 book ai didi

jquery - xmlHttp.getResponseHeader + 不适用于 CORS

转载 作者:行者123 更新时间:2023-12-03 21:41:08 25 4
gpt4 key购买 nike

我在 .NET 4 上有一个 asp.NET WCF。此服务用于对用户进行身份验证。我们正在提交用户名和密码,然后应该返回一个包含身份验证 cookie 的 HTTP header 。使用本地托管的测试页面,这可以正常工作。我现在正在尝试跨域访问 header 信息。我已在另一台计算机上安装了测试页并配置为调用 WCF。通话正常,通话中的“数据”回复正确。但是,我无法通过以下任一方式访问 header 信息:

alert(xmlHttp.getAllResponseHeaders());

alert(xmlHttp.getResponseHeader("Set-Cookie"));

使用 IE 中的调试器和 Firefox 的“实时 HTTP header ”插件,我可以看到正在返回 header 信息。

在我的全局 ajax 页面中,我设置响应来处理 CORS。

private void EnableCrossDomainAjaxCall()
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");


if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{

HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");

HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}

}

这是我用来调用服务的 AJAX:

$("#btnLogin").click(function(e) {
var geturl;
geturl = $.ajax({
// type: "POST",
type: "GET",
contentType: "application/json; charset=utf-8",
url: 'http://10.0.4.66/AuthenticationService.svc/Login?Name=test&password=pwsd',
// url: '../SecurityServer/AuthenticationService.svc/Login?Name=test&password=pwsd',
dataType: "jsonp",
error: function(request, status, error) {
alert('Error Occured');
},
crossdomain: true,
success: function(data, textStatus, xmlHttp) {
// alert(xmlHttp.getResponseHeader("Content-Type"));
document.write(xmlHttp.getResponseHeader("Content-Type") + "<br/>");
alert(xmlHttp.getAllResponseHeaders());
alert(xmlHttp.getResponseHeader("Set-Cookie"));
var headers = '';
var headerPair = xmlHttp.getAllResponseHeaders('wcfCookie').split("\r\n");
var output = '';
$.each(headerPair, function(key, line) {
var parts = line.split(':');

if (parts[0] == 'wcfCookie') {
ChocChip = parts[1]
return false
}

});
}
});

下面是我从“Live HTTP headers”中获取的 header 信息

Date: Mon, 04 Feb 2013 12:12:40 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
Access-Control-Allow-Origin: *
Set-Cookie: wcfCookie=8D38D5D6A0F138FEB595DD016F7694EDDF3E6757C82ED3D419F5047A5294974C1885487465CEC0A0BCC2B3802C7B03FF9F5370A05D4CCBDDDABCB1558C3816044BF4F78209BF38C6B1A7CAD34CD3C85C40B8515CFB1C2B2694BC78803D8DACB4
Content-Length: 65
Cache-Control: application/json; charset=utf-8
Content-Type: application/x-javascript

最佳答案

首先,一些背景知识:

您正在使用Access-Control-Allow-Headers,它指定允许客户端发送哪些请求 header ,但您未指定允许客户端读取哪些响应 header 。要允许客户端读取非简单响应 header ,需要使用Access-Control-Expose-Headers。来自 HTML5 Rocks CORS page :

During a CORS request, the getResponseHeader() method can only access simple response headers. Simple response headers are defined as follows:

  • Cache-Control
  • Content-Language
  • Content-Type
  • Expires
  • Last-Modified
  • Pragma

If you want clients to be able to access other headers, you have to use the Access-Control-Expose-Headers header. The value of this header is a comma-delimited list of response headers you want to expose to the client.

因此,鉴于新信息,您可能会这样做:

HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers", "Set-Cookie");

...但还有更多内容。

现在,实际答案:

这里还有一个更严重的问题:XHR 规范 explictily disallows reading Set-Cookie 。这是因为这在功能上是一种跨域 cookie 窃取攻击

假设域 A 向域 B 发出跨域请求。当域 B 设置 Cookie 时,它​​只是为域 B 设置域特定的 Cookie。域 A 尝试读取域 B 的 cookie 的任何行为都违反了 cookie 访问的同源策略。

我不了解 WCF,所以我不确定实际上执行您想要的操作的最佳方法,但我猜解决方案可能是传递身份验证 token 而不是通过域 A 读取并设置自己的 cookie(例如,X-WCF-Auth header ?)。

关于jquery - xmlHttp.getResponseHeader + 不适用于 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14686769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com