views:

315

answers:

2
  1. What is the difference between:

    HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value

and

System.Web.SessionState.HttpSessionState.SessionId

(while debugging, both of the values were always the same)

+2  A: 

ASP.Net uses that cookie to track the session across requests. They will always be the same value. For more details: http://msdn.microsoft.com/en-us/library/ms178581.aspx

AUSteve
+1  A: 

Well there exists more than one method of how ASP.NET handles a session. It's configurable by the web.config for example.

One of them is by cookies. Another one is by putting the session ID inside of the url. I boldly presume you won't be able to find the session ID in the cookie collection in that second case.

herzmeister der welten
Indeed, that cookie name is just an implementation detail and its name could change in a future version of ASP.NET, so you can't rely on it.
Eilon
To extend this, the SessionId property hides the implementation details. If you change the session settings to not use cookies, then SessionId would still provide the ID while the cookie would be blank.
Chris Lively
Yes I think you can also easily configure the actual cookie name for the session ID. All in all, never rely on such magic strings when programming in .net.
herzmeister der welten
or any other language
AUSteve