浏览器未设置 CORS cookie,响应中存在 cookie
CORS cookies not setting by browser , cookies are present in response
我正在向 node.js 服务器的 API 端点发送 http 请求,此响应包含 cookie 并显示在 mozilla 调试器中。我通过 localhost.org 或我的虚拟主机发送此请求,例如 myapp.example.com。
我也尝试了 AJAX XMLHTTPRequest 和 angular http
还要为 AJAX 添加 xhrFields: {withCredentials: true}
凭据:'include' 用于获取等
但是浏览器没有设置cookies,我试了2天了,这个问题还没有解决。
这是发送 ajax 请求的 util 函数
function sendServerRequest(url,method,data) {
return new Promise(function ( resolve ) {
resolve($.ajax({
url,
method: method || 'GET',
data,
xhrFields: {withCredentials: true},
crossDomain: true,
success: function ( data ) {
return data
},
error: function ( err ) {
try {
let responseStatus = err.responseJSON
if ( responseStatus.status === sessionExpires ) {
sessionExpireCall(responseStatus.message)
}
} catch ( e ) {
console.log('Failed to get response');
}
}
}));
})
}
原始响应
HTTP/1.1 200 OK
X-RateLimit-Limit: 13000
X-RateLimit-Remaining: 12996
Date: 2019-06-26 06:17:56
X-RateLimit-Reset: 1561533343
X-DNS-Prefetch-Control: off
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=15552000; includeSubDomains
X-Download-Options: noopen
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Access-Control-Allow-Origin: http://192.168.10.3
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type,
Authorization
Access-Control-Allow-Credentials: true
set-cookie: true
set-cookie: driverId=c81e728d9d4c2f636f067f89cc14862c; Max-Age=3600;
Domain=192.168.10.3; Path=/; Expires=Wed, 26 Jun 2019 07:17:56 GMT
set-cookie: driverName=kamran; Max-Age=3600; Domain=192.168.10.3; Path=/;
Expires=Wed, 26 Jun 2019 07:17:56 GMT
set-cookie: connect.sid=s%3AX- syECgzpFEZhh4d5B_xfStUoYPrO3p1.%2FPj%2BMo7UnaZto6OGoP%2BOjdKvrJ%2F3Fm%2Bn1BJ%2FXU7Qdh8; Domain=192.168.10.3; Path=/; Expires=Wed, 26 Jun 2019 07:17:56 GMT; HttpOnly
Content-Type: application/json; charset=utf-8
Content-Length: 34
ETag: W/"22-tpsDmOyD3m/F84/JvyRCwDCbeY8"
Connection: keep-alive
{"detail":"verified","status":200}
请求原始数据
GET http://localhost:3000/driverboard/verifycode?
mobile=923002222222&vcode=2609 HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Accept: */*
Origin: http://192.168.10.3
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
Referer: http://192.168.10.3/capptin-driverboard/verification.php
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: true
CORS 与 cookie 无关,CORS 与 HTTP headers 有关。它允许发出 cross-domain 请求(从一个域到另一个域。例如从 myapp.example.com 到 google.com)。 Cookie 只发送到同一域,例如从 myapp.example.com 您可以将 cookie 发送到同一域。example.com
您可以使用“/”作为路径将 cookie 附加到您的响应中。
在 C# 核心中添加 cookie。将其附加到响应:
context.Response.Cookies.Append(cookieNameAsString, cookieValue, new CookieOptions()
{
Expires = time,
Path = "/",
});
或在 javascript 中创建一个 cookie:
document.cookie = "cookieNameAsString='cookieValue'; max-age=time;";
我正在向 node.js 服务器的 API 端点发送 http 请求,此响应包含 cookie 并显示在 mozilla 调试器中。我通过 localhost.org 或我的虚拟主机发送此请求,例如 myapp.example.com。 我也尝试了 AJAX XMLHTTPRequest 和 angular http 还要为 AJAX 添加 xhrFields: {withCredentials: true} 凭据:'include' 用于获取等
但是浏览器没有设置cookies,我试了2天了,这个问题还没有解决。
这是发送 ajax 请求的 util 函数
function sendServerRequest(url,method,data) {
return new Promise(function ( resolve ) {
resolve($.ajax({
url,
method: method || 'GET',
data,
xhrFields: {withCredentials: true},
crossDomain: true,
success: function ( data ) {
return data
},
error: function ( err ) {
try {
let responseStatus = err.responseJSON
if ( responseStatus.status === sessionExpires ) {
sessionExpireCall(responseStatus.message)
}
} catch ( e ) {
console.log('Failed to get response');
}
}
}));
})
}
原始响应
HTTP/1.1 200 OK
X-RateLimit-Limit: 13000
X-RateLimit-Remaining: 12996
Date: 2019-06-26 06:17:56
X-RateLimit-Reset: 1561533343
X-DNS-Prefetch-Control: off
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=15552000; includeSubDomains
X-Download-Options: noopen
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Access-Control-Allow-Origin: http://192.168.10.3
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type,
Authorization
Access-Control-Allow-Credentials: true
set-cookie: true
set-cookie: driverId=c81e728d9d4c2f636f067f89cc14862c; Max-Age=3600;
Domain=192.168.10.3; Path=/; Expires=Wed, 26 Jun 2019 07:17:56 GMT
set-cookie: driverName=kamran; Max-Age=3600; Domain=192.168.10.3; Path=/;
Expires=Wed, 26 Jun 2019 07:17:56 GMT
set-cookie: connect.sid=s%3AX- syECgzpFEZhh4d5B_xfStUoYPrO3p1.%2FPj%2BMo7UnaZto6OGoP%2BOjdKvrJ%2F3Fm%2Bn1BJ%2FXU7Qdh8; Domain=192.168.10.3; Path=/; Expires=Wed, 26 Jun 2019 07:17:56 GMT; HttpOnly
Content-Type: application/json; charset=utf-8
Content-Length: 34
ETag: W/"22-tpsDmOyD3m/F84/JvyRCwDCbeY8"
Connection: keep-alive
{"detail":"verified","status":200}
请求原始数据
GET http://localhost:3000/driverboard/verifycode?
mobile=923002222222&vcode=2609 HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Accept: */*
Origin: http://192.168.10.3
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
Referer: http://192.168.10.3/capptin-driverboard/verification.php
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: true
CORS 与 cookie 无关,CORS 与 HTTP headers 有关。它允许发出 cross-domain 请求(从一个域到另一个域。例如从 myapp.example.com 到 google.com)。 Cookie 只发送到同一域,例如从 myapp.example.com 您可以将 cookie 发送到同一域。example.com
您可以使用“/”作为路径将 cookie 附加到您的响应中。
在 C# 核心中添加 cookie。将其附加到响应:
context.Response.Cookies.Append(cookieNameAsString, cookieValue, new CookieOptions()
{
Expires = time,
Path = "/",
});
或在 javascript 中创建一个 cookie:
document.cookie = "cookieNameAsString='cookieValue'; max-age=time;";