IIS Express doesn't keep session data

2019-05-15 07:50发布

问题:

I have an application that I run in Visual Studio 2013 under IIS Express server. The problem is that session data is not kept between pages and I find with null objects that weren't null the page before. If I compile the application and deploy it on a web server with IIS it works normally, so it must be something with IIS Express. I searched the config files in the document folders, but really don't know what it is.

Thanks, Luke

Edit 25/11: I'm debugging the code on the same machine witn VS 2013 and VS 2010 and I found where the code behaves differently. After the code behind has been executed and the page has been built, in VS 2010 the execution ends. In VS 2013 after the page has been built I get another call to the method context_BeginRequest(object sender, EventArgs e), which redirects the application to the starting page, where the instruction Session.Clear() clears the session. The problem now is: why is there this additional call to context_BeginRequest method? Why does it redirect to the starting page and not to the page it is currently in? Thanks, Luke.

回答1:

I was having a similar problem. My code was running fine on a production server, but when I debugged it on my test machine, the session would be null between saving and redirects.

When I set the session state to cookieless, <sessionState cookieless="true", the session variable would be retained. This was undesireable though because it adds the session name to the url.

Upon further review, I noticed a line in my system.web <httpCookies requireSSL="true" /> When I commented it out, everything worked as usual.

The problem was that my production IIS server was hosting the code using https://, while my test IIS Express server was only using http. So my unsecure cookies were getting discarded.