views:

214

answers:

2

For various reasons I am fed up with ASP.NET session state and I'm trying to do it myself (separate question coming soon related to why I'm fed up and whether it's feasible to do it myself, but for now let's assume that it is).

Security concerns aside, it seems like tracking sessions involves little more than storing a cookie with a guid and associating that guid with a small "sessions" table in the database, which is keyed on the guid and contains a small number of fields to track timeout and to link to the primary key in the user's table, for those sessions that are linked to registered users.

But I'm stuck on a detail with the cookie, in the case the user's browser is not set to accept cookies. It seems to me that each time a user accesses any page that has session state enabled, ASP.NET must determine whether the browser supports cookies. If there already is a session cookie sent with the request, obviously it knows cookies are accepted.

If not, it seems like it needs to check, which as I understand it involves trying to write a cookie and redirecting to a page that tries to read the cookie. So it seems, when a user with cookies turned off visits several pages of a site, that ASP.NET

(a) has to do this round-trip test for every page the user visits, or

(b) has to assume the browser accepts cookies and create a record with a (provisional) session id for the user on each page -- and if session state is supposed to be persistent, it seems it has to write that initial session id to the database on each page.

But (a) sounds crazy and (b) sounds crazy also, since we would quickly accumulate session ids for all these single-page sessions. So I'm thinking there must be some other trick/heuristic that is used to know when to do the round-trip test and/or when to actually create a record for the session.

Am I wrong to be perplexed?

(To be clear, I'm not talking about implementing a custom storage solution within ASP.NET's pluggable session state system. I'm talking about skipping ASP.NET's session state system entirely. My question is a more detailed version of this one: http://stackoverflow.com/questions/1142010/implementing-own-session-management-in-asp-net.)

+1  A: 

Well, cookies are a standard mechanism of web authentication. Do you have any reason at all why you wouldn't want to use them? Are you sure you're not trying to invent a problem where there isn't any problem?

Most serious websites I know of require the browser to accept cookies in order for the user to be authenticated. It's safe to assume that every modern browser supports them.

There's an interesting article about cookieless ASP.NET that you should read.

EDIT:

@o.k.w: By default the session state is kept by ASP.NET in-process (read: in memory). Unless told explicitly by the configuration to store the session in the database (SQL Server is supported out-of-the-box), this won't result in a database hit. The stale session states will get purged from the in-process storage. Unfortunately, with default ASP.NET settings every cookieless request will result in a new session being created.

Here's a detailed comparison of available session storage options: http://msdn.microsoft.com/en-us/library/ms178586.aspx.

Marcin Seredynski
@Mercin, I don't that's the concern of the OP. One of the issues is, do the web-server create a new session if the request do not contain a known session (or session-id)? If so, this will 'waste' the server resource if the clients are not-supporting cookies. Will the server then know that it doesn't need to 'care' about session-cookies for this particular cookies-less client? If so, how?
o.k.w
@Mercin, Thanks for address my point, appreciated :) +1
o.k.w
Thanks for this information but I don't think it answers my question.
M Katz
@M Katz: I thought I answered your question. Could you rephrase your question, please? I'm not sure what are you asking about now.
Marcin Seredynski
Thanks. Yes, I think you almost answered it, but I was focused on the specifics of how specifically it knows whether you are in a cookied or cookie-less situation, and how it does that without having to create all of these provisional session records for pages that never will join a session. I knew about cookieless sessions, but I was trying to understand if your goal is using cookies, but the browser doesn't support them, how you can avoid having the server hurt by that.
M Katz
The server configured to use cookies for session id doesn't have a 100% reliable way of telling whether a request coming without a cookie is a *new* session, or a *cookieless* request. The server will send the browser a session cookie, because it thinks it's a new session. As the browser doesn't support cookies, it won't sent it back, and each consequent request will be seen by the server as a *new session*... As RickNZ said, >99% of real users support cookies, so you don't really have to worry about the remaining <1%. I suggest you deal with cookieless sessions only if they become a problem.
Marcin Seredynski
A: 

Session behaviour is set through the sessionState element in web.config. In the sessionState element the HttpCookieMode can be set to one of UseUri, UseCookies, AutoDetect, UseDeviceProfile.

UseUri and UseCookies tell ASP.NET explicitly how to handle storing the session identifier. If UseDeviceProfile is used then the behavior is determined by whether the user agent supports cookies (not whether they are enabled or not).

AutoDetect is the interesting case that you are interested in. How ASP.NET is handling the auto detection is explained in Understand How the ASP.NET Cookieless Feature Works. In that article you will see that they have 5 different checks they do. One of the checks is, as you mention, to set a cookie and do a redirect to see if the cookie exists. If the cookie exists, then the browser supports cookies and the sessionID cookie is set. However, this is not done on every request because another check they do before tring to redirect is is check to for the existence of any cookies. Since after the initial set-cookie and redirect the sessionID cookie will be set then the existence of the cookie lets ASP.NET know that cookies are supported and no further set-cookie and redirects are required.

Tuzo
Yes, I think the algorithm in that link is mostly the answer to my original question. The key seems to be that it avoids doing the round-trip check on every page because after the first round-trip fails it modifies the URL and from then on the modified URL is the flag that cookies are not supported and no more checking is needed. It seems like there is still a basic unresolved question, however. If you set your mode to UseCookies (instead of AutoDetect), does it then do the round-trip on every page (since it's not allowed (?) to modify the URL)?
M Katz