Maintaining the same session after closing and reopening the browser

I am new to asp.net. I went to the site and created a session for userid, treating this session as s1. A cookie is added (c1) to the client’s site after 3 days.

Suppose if I close the browser without logging out and I use the same url again, then I found that the session is null, but I got a cookie (c1), after which I created a new session. But the s1 session still takes up memory on the server. This time, two sessions are on the same server, occupying memory.

I want to use an s1 session with a cookie (c1) - this is possible. or I want to delete the s1 session if the second time a request arrives.

The code I use is:

if (Session["UserInfo"] != null) { // code } else { HttpCookie HT = Request.Cookies["User"]; if (HT != null) { Session["UserInfo"] = HT["UserName"]; //Here new session is created while previous is already exist on server } else { //code } } 
+4
source share
2 answers

IE generates a new session identifier for each request. Therefore, when you close your browser, the old session identifier is lost, so the browser sends a request with a new session identifier. This is why you cannot get the session value.

But on the server, it takes up server memory, which is cleared by the server after the session ends.

Firefox sends a request with the same session ID for each request, and you can get your session ID even if you close your browser tab.

+1
source

A typical ASP configuration cookie with form authentication:

.ASPXFORMSAUTH expires: 30 minutes ASP.NET_SessionId Expiration: End of Session

Therefore, when you close the browser, you will lose the session, but you will not be logged out. When you re-open the browser, only ASP.NET_SessionId has expired.

If you want to create a cookie with persistent sessions, use this:

Create persistent cookies

0
source

All Articles