Solved

Session_Start firing upon every request

Posted on 2011-03-01
7
1,809 Views
Last Modified: 2012-05-11
Session_Start is firing upon every request in an asp.net Application after calling Session.Abandon() and FormsAuthentication.SignOut().

I have attached sample code that shows the behavior I am seeing. Follow the steps below with the sample code.
1: Set breakpoints on Session_Start and Session_End method in global.asax
2: Login into the website
3: Use links provided to navigate between two pages(Home and Page2).
4: Click Logout but don't close the browser
5: Repeat step 2-3

After second login you will see Session_Start and Session_End is called upon every page request i.e. when you navigate between home and page2. The most important thing is Session_End is called immediately after Session_Start before the page is shown.

Points to note:
1: I am doing manual logout in the logout.aspx.cs by calling FormsAuthentication.Signout() and also calling Session.Abandon.
2: If I comment out the Session.Abandon() in the logout.aspx.cs page, it works fine and Session_Start doesn't fire upon every request.
3: I have tried IE and FF.

There is something really small concept I am missing about Session here. Anyone has any ideas?

PS: I wasn't able to upload .asax file but here is the content of global.asax.

 login.zip
<%@ Application Language="C#" %>

<script runat="server">
    void Session_Start(object sender, EventArgs e) 
    {
        //set breakpoint
    }
    void Session_End(object sender, EventArgs e) 
    {
        //set breakpoint
    }
       
</script>

Open in new window

0
Comment
Question by:guru_sami
  • 4
  • 2
7 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 0 total points
ID: 35010644
The answer provided here by Alex B seems to be solving the problem.

But I am wondering why is this happening? My understanding is since the browser is not closed the same SessionID is used and that is something causing such recursive behavior.
0
 
LVL 16

Assisted Solution

by:Stephan
Stephan earned 100 total points
ID: 35014843
When you are setting the session.abandon, you will loose your sessionID, on every page request there will be a new sessionID untill you set a session that references to this sessionID.

I got this question myself a few weeks ago that from .net 2.0 always recreate a sessionID untill there is a session to maintain.

So in your case the following happens:

1. login, nothing is wrong
2. logout, session is abandoned
3. page_request, sessionid created (e.g. session_start)
4. page_unload, sessionid is lost (e.g. session_end)

step 3 and 4 will always be done untill a new session is set.

If there are any questions, I'd love to hear.
0
 
LVL 15

Expert Comment

by:crisco96
ID: 35016236
If you want the user to keep using the same session ID use Session.Clear() instead of Session.Abandon()
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 41

Author Comment

by:guru_sami
ID: 35018514
@Stephan,
The SessionID is remaining the same and that's because I am not closing the browser. And it remains the same even after setting some dummy session. SessionID changes only with new browser.

@crisco96,
No, it's not I want to reuse the SessionID because it always remains the same with or without Session.Abandon.

In general what I am looking for is what is causing this?
1: Setting a dummy session in Session_Start makes it work so what does it do?
2: It works fine if I change the framework to 4.0. So what change in 4.0 makes it work?
0
 
LVL 16

Expert Comment

by:Stephan
ID: 35018602
0
 
LVL 41

Author Comment

by:guru_sami
ID: 35018955
"..a new session ID is generated for each page request until the session object is accessed."

Yes, I see that but once you start using the session or even if you just define an empty Session_Start block in your Global.asax, the SessionID becomes constant.
You can try in by simply outputting SessionID in the default.aspx and Page2.aspx in the sample code I provided.
0
 
LVL 41

Author Closing Comment

by:guru_sami
ID: 35414386
Didn't get accurate answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Automatic sizing of HTML.TextAreaFor based on content 1 26
Tool Box 2 35
ADO.NET ENTITY DATA MODEL 3 32
Turn on intranet settings 1 40
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now