Asp.Net and Session – some Interesting Facts

Hi,

Yesterday I was discussion with my colleagues about session and a few interesting things popped up. So I thought I would share the same with all.

If we have the session state enabled and we do not store anything in the session then the session Id will change every time a new request is made. This also means that a new session is created every time. But the sate is never saved as there is nothing to save. Note the session_Start event will not fire for every request. The session_start event will only fire once.

Another interesting stuff about session Id is that it does not changes after we have called the Session.Abondon() method or when the session times out. Even though Session State expires but the session ID remains same. The session Id will last as long as the browser session does.

Session Class has two methods. Session.Abondon() and Session.Clear(). Both the methods are used to clear the data in the session. But there is one difference between them. If we use Session.Abondon() then Session_end event will be fired and session_start event will be fired on the next request. The same is not true for session.Clear() method.

Vikram

Share this post   Email it

Feedback

Posted on 9/30/2007 3:15:51 PM

Replied to your post in microsoft.public.dotnet.framework.aspnet.

Posted on 9/30/2007 8:20:53 PM

Hi,

The post is good but the last paragraph has few mistakes. One line is repeated and the last one perhaps has some missing words.

Thanks,
Raj

Posted on 9/30/2007 10:16:31 PM

Hi,

This is post is useful.Plz Always send new things.

Thanks
Reetesh

Posted on 9/30/2007 11:16:34 PM

Hi Raj,

Thanks for the comment. I have updated the Typo mistake :-).

Posted on 10/1/2007 1:18:14 AM

good post
Regards,
Jaipal

Posted on 10/1/2007 2:12:50 AM

hi.
thanx for this information as i am working with sessions and this will surly help me....

Posted on 10/1/2007 3:41:23 AM

Are you trying to describe the difference between Session.Abondon() and Session.Clear() or you have some other purpose as well?

Posted on 10/1/2007 5:37:03 AM

Vikram,

I am sorry but didn't get what you are trying to say with the first stmt of creating session on every request.

Also from the Web server prespective the difference between abandon and clear is that abandon removes the user session and creates a new on new request whereas clear only clears out the data exists in session. do let me know if I am wrong and please explain the first step.

Thanks Buddy,

Regards,
Dinesh Bisht

Posted on 10/1/2007 8:23:53 AM

HI Dinesh Bisht

In the first para i Was trying to tell that if we do not store any data in the session then nothing is saved in the session. So when a new request is made then a new session ID is generated. This will happen if there is no data in session.

And Yes the major difference between session.Abandon and Session.clear is what you understood

Posted on 10/1/2007 9:28:18 AM

Good work !!!!!

Posted on 10/1/2007 10:53:04 PM

Thank you for your comments.if you found any new updation share with me.

Posted on 10/2/2007 5:28:43 AM

Hi,

Thanks for the important updation regarding one of the most important features of ASP.NET. It is very useful.

Posted on 10/2/2007 7:41:40 AM

Hi Vikram
Your blog is really good. I want to strat a new blog. Can you please tell me, from where you got this .Net blog Software?
thanks for your help..
Vijay

Posted on 10/2/2007 11:59:31 AM

very nice and useful info... thanks for that...

Posted on 10/2/2007 9:40:47 PM

HI Vijay,

I have used a custom software for this blog. I used a starter kit and created on it. Now a dasy u can find many starter kik for the blog like blognet, dasblog if u search google.

Posted on 10/3/2007 5:37:52 AM

Hi Vikram
i am a frequent user of ur Blog.. i have read almost all ur stuff.. and really they are very usefull..
Now my point as u explain about Session_id.. My Question is: u are saying if we are not saving any thing in Session then for each request New SeesionID will create.. But Session_id creats only when Session_Start fires .. and Session_Start fires only once .. so how Diffrent Session_ID will cretae ... plz explain me if possible

Posted on 10/13/2007 3:14:48 AM

Hi,
I am facing a problem in web application, and that is commented below:

like if i have one IE window opened and again we open the another window by Ctrl + N or from FILE > NEW > WINDOW.

if i login in first window with one user id, and in another window by another user id, then what happens is that the second IE windows session goes into first window.

And it does not happen when we open different-2 window by clicking on icon placed on desktop.

If you have any answer please reply me on my email.

Thanks

Rahul
09313770772

Posted on 10/14/2007 5:41:27 AM

HI Rahul,

This is a typical feature of IE. In IE if you open a new window with the help of ctrl + N or file new window, then the second window opened shares the session of the first window. and this is the reason of the behaviour u are getting.

Hope this helps.

Posted on 7/5/2008 1:38:17 AM

Nice stuff. keep it up :)

Useful articles

Posted on 7/29/2008 1:44:08 PM

So helpful, thanks for sharing your experience :)

Posted on 7/29/2008 1:44:34 PM

So helpful, thanks for sharing your experience :)

Posted on 3/11/2010 12:22:24 PM

HI, Vikram
Nice blog u have written here.....from here i only i have know that Session ID will be recreated on every request if we dont have anything in session.......

Thanks
Vimal

Posted on 3/16/2010 9:15:09 PM

Hi Vikram,
Thanks for sharing the info - session_start is fired only once.
Do u know how could I implement controlling concurrent users to web application. I have used application variable which is incremented in Session_start and decremented in Session_end (I know if user close browser, session_end is not fired, but this is not an issue for me).

I am wondering if there is a way by which I can increment the application variable at the start of each request

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Copyright © 2006 - 2010 Vikram Lakhotia