5

I think I may have found a problem with ASP.NET MVC and it's event pipeline. In particular, I am finding that Session_Start is being called multiple times, each containing a new SessionID.

Here's the step-by-step process:

  1. Open VS2010
  2. File | New Project
  3. ASP.NET MVC 3 Web Application, accept default name, click OK
  4. Select Internet Application (although I don't think it matters really), click OK
  5. When finished creating, edit the Global.asax.cs file
  6. Add the following method (yes it's empty):

    protected void Session_Start() { }

  7. Set a breakpoint in the method

  8. Debug
  9. Notice that the breakpoint is caught twice before displaying the page. If you watch "Session.SessionID" when the breakpoints are caught, you will see that the session id is new each time.
  10. Once you get to the home page, click on the "Home" or "About" tab link.
  11. Session_Start will be fired again, this time with a new SessionID.
  12. Continue execution, and any subsequent actions will no longer fire Session_Start.

I tried the same thing on a standard ASP.NET Web Application (not MVC), and Session_Start only fired once.

I'm pretty sure I'm not doing something wrong here, as I am using the default project templates, and the only code that is being modified is the Global.asax.cs file, to add the Session_Start method.

I am using IIS Express, but I've repeated the above steps using the "Cassini" web server (Visual Studio Development Server), with the same result.

Any advice?

UPDATE

I decided to use Fiddler to inspect the HTTP traffic during my debug session. It seems that:

  1. The first Session_Start is fired when I am requesting the "/" URL. This seems reasonable. The SessionID generated at that time is then written in the response to the browser. Again, seems reasonable.
  2. Fiddler then shows requests/responses for the *.js and *.css files. All successes. None of those fire off Session_Start. Good so far.
  3. Then Fiddler shows that a request has been made for "/favicon.ico". At this time, Session_Start fires, and generates a new SessionID... I continue.
  4. On Fiddler, it shows that the "/favicon.ico" file was not found (404). The webpage is displayed. I click on the "Home" link.
  5. The URL "/" is requested and response is OK in Fiddler. But then, another "/favicon.ico" file is requested, and again Session_Start fires with a new SessionID... I continue.
  6. All subsequent requests have responses, and the browser stops asking for "/favicon.ico".

I made note of each of the three SessionID's generated, and it seems the one that the browser holds on to is the first one. So when we get to step 6 above, and everything seems to work, it's actually using the very first SessionID that was generated.

So... I decided to host a "favicon.ico" file. I placed the ico file in the root of the project, and started my debug session again. This time, Session_Start only fires once. "/favicon.ico" was served successfully (200).

So... I guess it is working the way it should in a sense... But why do calls to "/favicon.ico" fire off the Session_Start event???? Shouldn't I have the choice to NOT host a favicon?

ASIDE: I tried all the above in an ASP.NET (not mvc) project, and it did not have the same problem, even though there was no favicon.ico file hosted by a default "ASP.NET Web Application" project.

Lee DeLapp
  • 341
  • 1
  • 2
  • 9
  • 1
    Do you have configure your browser to not accept cookies? – Bruno Costa Aug 17 '11 at 18:41
  • This code is run on my local machine, and the browser does allow cookies. I tried the same thing using a different browser (Chrome instead of IE) and I got the same result. – Lee DeLapp Aug 17 '11 at 18:44
  • I followed your instructions and I could not reproduze your problem... Check how the cookies is being saved in your browser. Do you have 1 cookie for each link? – Bruno Costa Aug 17 '11 at 19:11
  • See the update to the question above. – Lee DeLapp Aug 17 '11 at 20:07
  • 1
    Did you try to ignore the route for the favicon? http://weblogs.asp.net/gunnarpeipman/archive/2009/02/26/asp-net-mvc-ignore-requests-to-favicon-ico.aspx – Tz_ Aug 17 '11 at 23:15
  • I know this is a few yeas after the fact, but i came across this post when running into the exact same issue. DEBUG THE HTTPS URL (this happened to me because requireSSL was set to true, and i wasn't browsing the ssl url, hence the session got thrown away on every redirect.) – VisualBean Oct 12 '15 at 08:12

5 Answers5

6

I kinda had this problem for a while, and finally I realised that it was because there was some http/https shenanigans going on... looks like it destroys and recreates your session if you flip the ssl around like that and you have

<sessionState mode="InProc" sqlCommandTimeout="3600" timeout="120" cookieless="false" />
<httpCookies httpOnlyCookies="true" requireSSL="true" />

Possibly a trap for new players or people who are really tired and not paying attention! :) Just FYI in case this helps anyone...

nojedi
  • 61
  • 1
  • 2
  • 2
    The problem is properly because you're debugging without ssl, meaning that the browser will throw away the session each time because it has been told to (as long as it is not SSL) - if you set "Enable SSL" in the project properties, and browse the ssl url instead, everything will work. – VisualBean Oct 12 '15 at 08:08
  • I see the same. When using Chrome, and running the site locally without ssl, Session_Start() will be called for every request, even static files like css and js. Interestingly, it works fine with Firefox. – VRPF Oct 09 '18 at 11:35
  • You're a savior...this is relevant even after 8 years. Thank you that you made me think on the lines of http/https. I had exactly this issue and was trying to find an answer desperately. – shivam Apr 12 '21 at 12:24
2

I think I've come to a point where I have a couple of solutions (albeit both seem 'hacky' to me), so I think I'll accept these and move on.

Got a comment from @Tz_ above that mentioned I should ignore the route for the favicon file. That's essentially what I'll be doing. (kudos @Tz_!)

Came across the following post, (among others). It describes a problem that when the browser requests a "/favicon.ico" file from an ASP.NET MVC site, the MVC stack is mistakingly trying to look for and instantiate a controller. I'm wasn't sure if that was true or not for my situation, but the answer suggested adding the following route entry:

routes.IgnoreRoute("favicon.ico");

I gave it a shot (added the above), and that fixed it!

So, I still don't know why "/favicon.ico" request has a mistaken identity in MVC, but I know how to fix it in my situation. Either:

  • Host a favicon,
  • or add an ignore route entry.

Again, both seem like hacks to me, as I think this is something controller factories should be capable of handling gracefully. IMHO

Community
  • 1
  • 1
Lee DeLapp
  • 341
  • 1
  • 2
  • 9
  • The Favicon post you list also states it's only Cassini (the VS built in web server) that has the favicon problem, not IIS-Express or the real IIS. Even so, the ASP.NET MVC QA team and I have been unable to reproduce your problem. – RickAndMSFT Oct 02 '11 at 18:12
  • I'm having the same problem. My session gets ended and restarted on the redirect. Adding the IgnoreRoute did not fix it. – Seth Jul 03 '13 at 19:03
1

Reason you are getting Session_Start firing each time is because you have <httpCookies requireSSL="true" /> in <system.web> in your Web.Config remove this and you are good to go.

Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
0

This happened to me when I had some <img> in my pages with a wrong "src" attribute. Putting a valid path in "src" solved my problem.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
LNyarla
  • 454
  • 1
  • 8
  • 23
0

I can't reproduce this problem. I've tested on ASP.NET MVC 3/Tool Update, Win08/R2/SP1 and Win7/SP1 using IIS 7.5, Cassini and IIS Express. I see the 404 favicon request in Fiddler, but the break point is not hit for favicon. I tested with IE9, the current FF and Chrome. Each time I hit the site with a new browser, Session_Start() is called and I see the new session ID. I work for Microsoft so I'd like to know how to reproduce this problem.

RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78
  • I have Win7Ult/Sp1, ASP.net MVC 3 with the tools update. VS2010Pro/Sp1. I tested with IE9 and Chrome. Problem seems pretty consistent on my system. I do have another system at another location which I can check later to see if I have the same issues. Also, I'm using Autofac MVC3 integration, which I've disabled just to see if that was causing the problem (because it might be overriding the controller factory??? not sure), but it wasn't (latest NuGet package on that). – Lee DeLapp Aug 18 '11 at 19:29