我正在尝试使用新的Fetch API但是遇到了Cookie问题.具体来说,在成功登录后,将来的请求中会有一个Cookie标头,但是Fetch似乎忽略了这些标头,而我使用Fetch发出的所有请求都是未经授权的.
是因为Fetch还没有准备好,或者Fetch不能与Cookies一起使用?
我用Webpack构建我的应用程序.我也在React Native中使用Fetch,它没有相同的问题.
如何共享cookie跨源?更具体地说,如何将Set-Cookie标题与标题结合使用Access-Control-Allow-Origin?
这是我的情况的解释:
我正在尝试localhost:4000为托管在其上的Web应用程序中运行的API设置cookie localhost:3000.
我似乎在浏览器中收到了正确的响应标题,但不幸的是它们没有任何效果.这些是响应标头:
HTTP/1.1 200 OK Access-Control-Allow-Origin: http://localhost:3000 Vary: Origin, Accept-Encoding Set-Cookie: token=0d522ba17e130d6d19eb9c25b7ac58387b798639f81ffe75bd449afbc3cc715d6b038e426adeac3316f0511dc7fae3f7; Max-Age=86400; Domain=localhost:4000; Path=/; Expires=Tue, 19 Sep 2017 21:11:36 GMT; HttpOnly Content-Type: application/json; charset=utf-8 Content-Length: 180 ETag: W/"b4-VNrmF4xNeHGeLrGehNZTQNwAaUQ" Date: Mon, 18 Sep 2017 21:11:36 GMT Connection: keep-alive
此外,Response Cookies当我使用Chrome开发者工具的"网络"标签检查流量时,我可以看到cookie .但是,我看不到在"应用程序"选项卡下设置的cookie Storage/Cookies.我没有看到任何CORS错误,所以我假设我错过了其他的东西.
有什么建议?
我正在使用React-Redux应用程序中的请求模块/signin向服务器上的端点发出请求.对于服务器我使用快递.
快递服务器:
res.cookie('token', 'xxx-xxx-xxx', { maxAge: 86400000, httpOnly: true, domain: 'localhost:3000' })
在浏览器中请求:
request.post({ uri: '/signin', json: { userName: … 我可以在 PostMan 上执行以下操作
1) POST 方法登录到公司服务器。2)在公司服务器上以登录用户身份发出其他请求。
我创建了一个 nodejs 应用程序来与公司服务器进行通信。我正在使用 axios 库进行上述通信。
登录公司服务器后,其他任何电话都无法识别我为授权用户。
我可以反过来在 axios 上重新创建以具有该会话持久性的区别是什么?
我正在为 node.js 和 react.js 使用 express 构建登录系统。在我的后端,当用户登录时,它会创建一个 cookie。当我转到网络 > 登录时,我可以看到:
设置 Cookie:user_id=s%3A1.E%2FWVGXrIgyXaM4crLOoxO%2Fur0tdjeN6ldABcYOgpOPk;路径=/;HttpOnly; 安全的
但是当我转到 Application > Cookies > http://localhost:3000 时,那里什么也没有。我相信这是因为当我从客户端发出发布请求时,我不允许凭据正确通过。我该怎么做?请让我知道我是否可以以任何方式改进我的问题。
//Login back-end
router.post('/login', (req, res, next) => {
if(validUser(req.body)) {
User
.getOneByEmail(req.body.email)
.then(user => {
if(user) {
bcrypt
.compare(req.body.password_digest, user.password_digest)
.then((result) => {
if(result) {
const isSecure = process.env.NODE_ENV != 'development';
res.cookie('user_id', user.id, {
httpOnly: true,
secure: isSecure,
signed: true
})
res.json({
message: 'Logged in'
});
} else {
next(new Error('Invalid Login'))
}
});
} else {
next(new Error('Invalid …Run Code Online (Sandbox Code Playgroud) 我有一个基于 Nuxt/vuejs 构建的表单。在 django 的后端,启用了 CSRF 保护,现在需要 Api 中的两件事调用X-CSRFToken作为标头和csrftoken作为 Cookie ,我通过 Postman 调用 Api 来测试 Api,这工作正常,但在 Vue 的情况下它没有通过 post 请求发送 Cookie 让我向您展示我的代码
axios 发布请求
const headers = {
"X-CSRFToken": "some token",
"Cookie": "csrftoken=some token",
}
await axios.post(`onboarding/first-name-last-name-email/`, {
"first_name": "uneeb2",
"last_name": "sad",
"email": "asdsa@asd.colm"
}, {
headers: headers
}, {
withCredentials: true
})
Run Code Online (Sandbox Code Playgroud)
我也尝试过Fetch是否也有同样的问题
fetch(
'https://staging.goqube.io/api/onboarding/first-name-last-name-email/',
{ credentials: 'include' ,method: "POST",headers:headers} // could also try ''
).then(res => {
if (res.ok) return res.json()
// …Run Code Online (Sandbox Code Playgroud) 我在 Heroku 上托管我的 Express API ,在 Netlify 上托管我的客户端客户端。当我在本地尝试注册路由时,我的 cookie 已定义且路由有效。然而,当它投入生产时,cookie 总是返回未定义,并且我的 API 超时。
\n\n请注意,cookie 已从后端成功发送。I\xe2\x80\x99m 能够在开发工具中查看它。此外,cookies,get() 返回一个空对象。
\n\nI\xe2\x80\x99m 使用 Js-cookie。
\n\n我在 Gatsby 中使用 js-cookie。我正在使用 CSURF 来表达 cookie。
\n\n后端:
\n\n//CSURF Config\napp.use(csurf({ cookie: true }));\n\n\n//Route that generates CSRF Cookie\napp.get("/getToken", (req, res) => {\n res.cookie("XSRF-TOKEN", req.csrfToken());\n res.end();\n });\nRun Code Online (Sandbox Code Playgroud)\n\n前端:
\n\n我包括整个注册功能。请注意,这是两个端点调用,一个用于检索 cookie,另一个用于创建用户记录。
\n\n userSignUp = async (email, password, resetForm) => {\n console.log("THis is a test of the emergency..")\n await fetch(process.env.API + "getToken", {\n …Run Code Online (Sandbox Code Playgroud) $ node -v v10.15.0
"axios": "^0.19.2",
Run Code Online (Sandbox Code Playgroud)
我试图将 cookie 保留在响应标头字段“set-cookie”中 - 就像浏览器一样。我曾经使用过这个模块(https://www.npmjs.com/package/request)并且有一个request.defaults({jar: true})效果很好的选项。
对于 axios,我要求它{withCredentials: true}可以完成这项工作 - 但它不能。
这是一个示例代码:
$ node -v v10.15.0
"axios": "^0.19.2",
Run Code Online (Sandbox Code Playgroud)
控制台结果如下:
axios({ url: 'https://google.com/', method: 'get', withCredentials: true })
.then((res) => {
console.log('res.headers = ', res.headers);
})
.catch((err) => {
console.log('ERROR >>>> ', err);
});
axios({ url: 'https://google.com/', method: 'get', withCredentials: true })
.then((res) => {
// console.log("res.headers = ", res.headers);
console.log('REQUEST HEADERS: ', res.request._header);
})
.catch((err) => {
console.log('ERROR …Run Code Online (Sandbox Code Playgroud)