Javascript: Read out session id from cookie

て烟熏妆下的殇ゞ 提交于 2019-12-19 04:08:42

问题


for websockets I have to expose my sessionid from the cookie.

I have searched a bit and found that I should be able to access cookies by:

console.log(document.cookie);

unfortunatly this doesn't work or better document.cookie contains an empty string even chrome itself shows me the cookie also authentication works.

Can it be that chrome hides the cookie for javascript?


回答1:


That can happen if the server is configured to send the session cookie with the HttpOnly flag. This way the cookie becomes invisible/inaccessible to client side scripting languages like JS.

To achieve your concrete functional requirement, either reconfigure the server to not do so, or look for alternate means, e.g. setting a custom cookie (without the HttpOnly flag, of course), or letting the server side view technology dynamically print the current session ID as a JS variable or as an attribute of some HTML element so that JS can access it by traversing the HTML DOM.



来源:https://stackoverflow.com/questions/11924439/javascript-read-out-session-id-from-cookie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!