Storing Credentials in Local Storage

后端 未结 4 1563
陌清茗
陌清茗 2020-12-15 04:13

Could I securely use local storage instead of cookies to store session credentials?

Would I need to store an encrypted hash??

EDIT: Would this be secure eno

4条回答
  •  死守一世寂寞
    2020-12-15 04:48

    If you're going to be using local storage, why store user credentials or anything derived from them at all?

    What I've been looking into doing is:

    Upon successful login, generate a completely random string unrelated to user credentials and store that in the database, along with an expiry date. I would then pass that string to my js to be stored in local storage.

    From then on, so long as that local storage credential matches the database one and the timeout has not expired, I automatically consider them logged in.

    This way, there is no risk concerning the exposure of the user's credentials from local storage. However, with this temporary unique string essentially functioning as a sessionID, you will still to need to be aware of and take precautions against the risks associated with session hijacking.

    In any case, my understanding is that local storage is as secure as the server behind your site is. By that I mean local storage is only accessible via scripts coming in through your own domain, so you're safe so long as the only front code running is your own.

提交回复
热议问题