Saurabh Mahajan
Saurabh Mahajan

Reputation: 2969

Maintaining same session after browser closes and reopens

I am new to asp.net. I login the site and created session for userid consider this session as s1. Add an cookie (c1) to client site with expiration of 3 days is added.

Suppose if i close the browser without logout and again i use the same url then i found session is null but i got cookie (c1) then i created a new session. But session s1 is still occupies memory on server. Means this time two sessions are on the same server occupying memory.

I want to use session s1 with the cookie (c1) - is it possible. or i want to delete session s1 if second time the request comes.

The code I used 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
        }
    }

Upvotes: 4

Views: 3556

Answers (2)

semao
semao

Reputation: 1757

Typical ASP configuration cookie with Forms Authentication:

.ASPXFORMSAUTH expires: 30 min ASP.NET_SessionId expires: At end of session

So when you close your browser, you will loose session but you won't be logged off. When you reopen browser only ASP.NET_SessionId is expired.

If you want to create persistent session cookies use this:

Create Persistent ID Cookies

Upvotes: 0

Pavan Tiwari
Pavan Tiwari

Reputation: 3187

IE generate a new session id on each request. So when u close your browser old Session id is get lost, so browser sends request with new session id. That's why u are not able to get value of session.

But on server it occupies server memory , which is cleared by server after session time out.

Firefox sends request with same session id on each request, u can get your session Id even if u close your browser tab.

Upvotes: 1

Related Questions