I am developing a web application using C#
and javaScript
. I need to save user data in the browser so that the user once login need not to login on a new tab in same browser. But if he close the browser the data should be cleared.
I searched online and could not found a solution.
I tried using document.cookie
but they are not cleared when browser is closed. The same problem is with localStorage
.
I tried sessionStorage
but it is cleared when the tab is closed.
I also tried
$(window).on('unload', function (e) {
----
});
But the unload
function is clearing data on reload or tab closing.
Can anyone please give me a solution for this?
EDIT [ solved ]
The problem was with my Cookie
creation.
I created a persistent cookie
instead of session cookie
. When I removed the expires
from my cookie the problem is solved. Now it is clearing when browser is closed and will keep the user data even if the tab is closed.
thank you all for your help.