如何使用window.fetch()与httponly cookie或基本auth

问题描述 投票:0回答:3
fetch('/something', { headers: { Cookie: document.cookie } })

但这对httponly cookies不起作用。

okay,我在Mozilla开发人员网络上阅读了更多内容并尝试了凭证选项后发现。

看起来像凭据选项是我应该寻找的。

fetch('/something', { credentials: 'same-origin' }) // or 'include'

javascript dom cookies basic-authentication fetch-api
3个回答
65
投票

这里有三点:

如果您提出交叉启示请求,请设置

Access-Control-Allow-Credentials: true

。使服务器接受您的cookie。

4
投票

将凭据选项设置为cross-Origin请求或同一原始请求

include
  1. 设置了您检索的两个请求并发送cookie
    的选项。

  2. 我在登录页面上有同样的问题
    
    same-origin
    但是,当我尝试从服务器获取数据时,它无法识别身份验证的用户

  3. credentials
  4. 我可以在网络选项卡中看到到帐户时收到的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); } }

0
投票
任何帮助都非常感谢

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.