2

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.

Jinto Jacob
  • 385
  • 2
  • 17
  • https://stackoverflow.com/questions/11505872/method-to-store-data-until-browser-is-closed – PepitoSh May 29 '18 at 06:45
  • Possible duplicate of [Method to store data until browser is closed](https://stackoverflow.com/questions/11505872/method-to-store-data-until-browser-is-closed) – gotnull May 29 '18 at 06:50
  • Depending on the user agent (= the browser), the tab might be running in a seperate process. You have no guarantee that a tab will be conceptually different than a browser window. This is what you should get used to in web programming: you have limited control over the client. – Sefe May 29 '18 at 06:50
  • 1
    session cookies, when done correctly, will delete when browser is closed. you can create cookies with `document.cookies` that are either persistent or session, it is likely you are creating persistent ones. Typically, I don't do this from the front-end. In a server response header you tell the client to create a session cookie. https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies – Brett Caswell May 29 '18 at 07:03
  • "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" that is not typical behavior of someone who is logged in. Something is misconfigured in your webhost/webapp. – Brett Caswell May 29 '18 at 07:08
  • What type of server side code are you using? Webform? MVC? – Hooman Bahreini May 29 '18 at 07:11
  • @Hooman I am using angularjs MVC on client side. – Jinto Jacob May 29 '18 at 07:39
  • you have this question tagged as c#. which is why @Hooman asked that, but It actually doesn't matter what type of web project you have ('webform', 'mvc'), the underline issue here is ASP.NET misconfiguration. it is likely that you need to enable basic authentication and set the cookie name. – Brett Caswell May 29 '18 at 07:44
  • Perhaps a different approach, but you could use Asp.Net Identity which handles authentication, authorization for you: https://learn.microsoft.com/en-us/aspnet/identity/overview/getting-started/introduction-to-aspnet-identity – Hooman Bahreini May 29 '18 at 07:48
  • @BrettCaswell thank you for the comment. The problem was that I was creating persistent cookies. The issue is solved when I remove the expiry date for cookies. – Jinto Jacob May 29 '18 at 08:05
  • Possible duplicate of [How do I log a user out when they close their browser or tab in ASP.NET MVC?](https://stackoverflow.com/questions/23632725/how-do-i-log-a-user-out-when-they-close-their-browser-or-tab-in-asp-net-mvc) – TAHA SULTAN TEMURI May 29 '18 at 08:06
  • 1
    Look @jintopullolickal I can already tell you that your implementation here is potentially a security vulnerability and may lead to bugs and other forms of security vulnerabilities (aka not making endpoints perform Authorization checks). This entire concept of Authentication is a mechanism between client and server, in a request response scenario. If your server does not consider the user authenticated, but your client framework does - it is a problem. Good luck to you. – Brett Caswell May 29 '18 at 08:16

2 Answers2

0

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.

Jinto Jacob
  • 385
  • 2
  • 17
-1

Server side: If you want to maintain value within page use viewsate in c#. move to next page means use C# session.

Client side: If you want to maintain value in client side use document.cookies or sessionstroage, localstorage.

Angappan.S
  • 90
  • 9