fetch('/something', { headers: { Cookie: document.cookie } })
okay,我在Mozilla开发人员网络上阅读了更多内容并尝试了凭证选项后发现。看起来像凭据选项是我应该寻找的。
fetch('/something', { credentials: 'same-origin' }) // or 'include'
这里有三点:
如果您提出交叉启示请求,请设置
Access-Control-Allow-Credentials: true
将凭据选项设置为cross-Origin请求或同一原始请求
include
设置了您检索的两个请求并发送cookie的选项。
我在登录页面上有同样的问题
same-origin
但是,当我尝试从服务器获取数据时,它无法识别身份验证的用户
credentials
我可以在网络选项卡中看到到帐户时收到的cookie - 但我在chrome
中无法在应用程序选项卡=> cookie中看到它。 我正在将.NET核心身份与httponly cookies身份验证机制一起使用 - 这是程序中的CORS设置。
var response = await fetch(BASE_URL + 'login?useCookies=true', {
method: 'POST',
credentials: 'include',
body: JSON.stringify({email, password}),
headers: {'Accept': 'application/json', 'Access-Control-Allow-Credentials': 'true', 'Content-Type': 'application/json'}
});
if(response.status==200)
{
localStorage.setItem('user', email);
location.replace('index.html');
}
我的回应是我的回应const userInfo = async () => {
let response = await fetch(BASE_URL + 'account/user-info', {
method: 'GET',
credentials: 'include',
headers: {'Accept': 'application/json', 'Access-Control-Allow-Credentials': 'true', 'Content-Type': 'application/json'}
})
if(response.status ==200)
{
var jsonResponse = response.json();
console.log(jsonResponse);
}
}