Javascript AngularJS 中会话存储、本地存储和 Cookie 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26172976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Session Storage, Local Storage and Cookies in AngularJS
提问by GeekOnGadgets
I would like to dig down into Angular, and for that i would like to know the difference between Session Storage, Local Storage and Cookies.
我想深入研究 Angular,为此我想知道会话存储、本地存储和 Cookie 之间的区别。
Problem Questions ---
问题 问题 ---
1) $windows.sessionStorage can be used to store user session but the problem with it is, when you open something in a new tab it again ask the user to login.
1) $windows.sessionStorage 可用于存储用户会话,但它的问题是,当您在新选项卡中打开某些内容时,它再次要求用户登录。
2) Will Local Storage would be a solution to problem question 1 ? and if so, does that mean i need local storage and session storage both in my app or local storage will act as session storage as well.
2) Local Storage 会解决问题 1 吗?如果是这样,这是否意味着我需要在我的应用程序中使用本地存储和会话存储,或者本地存储也将充当会话存储。
3) I am also working on Remember me on my login form - is it safe to store password and email of the user in the local storage for this, if not what is best way to do remember me in angular
3)我也在研究在我的登录表单上记住我 - 为此将用户的密码和电子邮件存储在本地存储中是否安全,如果不是,以角度记住我的最佳方法是什么
4) Cookies are great, but does corporate companies allow them on there browser?
4) Cookies 很棒,但是公司公司允许它们在浏览器上使用吗?
Hoping to find decent answers
希望找到合适的答案
Thanks
谢谢
回答by SnareChops
1) It is correct that sessionStorageis temporary, and it has been designed to do so.
1)sessionStorage暂时是正确的,并且它已经被设计为这样做。
2) Local storage will solve the issue of the login going away with a new browser session being opened or after waiting a long time, but no, localStoragewill not act as a session cookie for browser requests.
2)本地存储将解决打开新浏览器会话或等待很长时间后登录消失的问题,但不会,localStorage不会充当浏览器请求的会话cookie。
3) Many different server side applications support encryption and tamper-resistant cookie support for applications. That being said, it is always best not to store user passwords in the client, maybe a token perhaps that your server will recognize and be able to decrypt/decode and look up the correct user record.
3) 许多不同的服务器端应用程序支持应用程序的加密和防篡改 cookie 支持。话虽如此,始终最好不要在客户端中存储用户密码,也许是一个令牌,也许您的服务器将识别并能够解密/解码并查找正确的用户记录。
4) I would say nowadays yes, cookies are generally accepted to be safe, however that is always a possibility, and depending on your clients or audience you may have an issue there. Also sessions won't work if cookies are disabled in the browser. (Though my outlook on this is speculation on a general population, ie: don't quote me on that)
4)现在我会说是的,通常认为 cookie 是安全的,但这总是有可能的,并且根据您的客户或受众,您可能会遇到问题。如果在浏览器中禁用 cookie,会话也将无法工作。(虽然我对此的看法是对一般人群的推测,即:不要引用我的话)
My recommendation for your needs is to set a session variable when the user encounters the page. Then store the result in localStorage or with a cookie, and then when the user returns to the application after the session has died, have some architecture set up to re-authenticate and re-assign the session automatically.
我对您的需求的建议是在用户遇到页面时设置会话变量。然后将结果存储在 localStorage 或 cookie 中,然后当用户在会话结束后返回应用程序时,设置一些架构来自动重新验证和重新分配会话。
Hope this helps!
希望这可以帮助!
Edit:Session Cookiesare shared between browser tabs within the same window. However Session Storagehas been pointed out not to be.
编辑:会话 Cookie在同一窗口内的浏览器选项卡之间共享。但是会话存储已被指出不是。
回答by Paul B. Hartzog
localstorage will work across tabs:
localstorage 将跨选项卡工作:
There is a demo here:
这里有一个演示:
http://www.undefinednull.com/2014/02/25/angularjs-real-time-model-persistence-using-local-storage/
http://www.undefinednull.com/2014/02/25/angularjs-real-time-model-persistence-using-local-storage/

