HttpContext.Current.Session & HttpContext.Current.Cache questions
-
I have a simple test web application with a single Default.aspx page with 2 buttons having the following handlers: What surprises me is I expected to get the same session ID between successive clicks of either buttons but I get different session ID's each time. I expected this to be different clicking from 2 different iexplorer instances and more so between an iexplorer and chrome / firefox. Also, from this experiment, it is obvious that HttpContext.Current.Cache is shared among clients as I get the values inserted from prior requests from different browsers.
protected void \_btnAdd\_Click(object sender, EventArgs e) { int cacheCount = HttpContext.Current.Cache.Count; Random rnd = new Random(); HttpContext.Current.Cache\[cacheCount.ToString()\] = TextBox1.Text; int intLessThanCacheCount = rnd.Next(cacheCount); \_lblCacheIndex.Text = intLessThanCacheCount.ToString(); \_lblCacheValue.Text = (string) HttpContext.Current.Cache\[intLessThanCacheCount.ToString()\]; lblSessionId.Text = HttpContext.Current.Session.SessionID; ViewState\["Value"\] = TextBox1.Text; } protected void \_btnGet\_Click(object sender, EventArgs e) { int cacheItems = HttpContext.Current.Cache.Count; Random rnd = new Random(); int intLessThanCacheCount = rnd.Next(cacheItems); \_lblCacheIndex.Text = intLessThanCacheCount.ToString(); \_lblCacheValue.Text = (string)HttpContext.Current.Cache\[intLessThanCacheCount.ToString()\]; lblSessionId.Text = HttpContext.Current.Session.SessionID; lblSessionValue.Text = ViewState\["Value"\].ToString(); }
---------------------------------------------------------- Lorem ipsum dolor sit amet.
-
I have a simple test web application with a single Default.aspx page with 2 buttons having the following handlers: What surprises me is I expected to get the same session ID between successive clicks of either buttons but I get different session ID's each time. I expected this to be different clicking from 2 different iexplorer instances and more so between an iexplorer and chrome / firefox. Also, from this experiment, it is obvious that HttpContext.Current.Cache is shared among clients as I get the values inserted from prior requests from different browsers.
protected void \_btnAdd\_Click(object sender, EventArgs e) { int cacheCount = HttpContext.Current.Cache.Count; Random rnd = new Random(); HttpContext.Current.Cache\[cacheCount.ToString()\] = TextBox1.Text; int intLessThanCacheCount = rnd.Next(cacheCount); \_lblCacheIndex.Text = intLessThanCacheCount.ToString(); \_lblCacheValue.Text = (string) HttpContext.Current.Cache\[intLessThanCacheCount.ToString()\]; lblSessionId.Text = HttpContext.Current.Session.SessionID; ViewState\["Value"\] = TextBox1.Text; } protected void \_btnGet\_Click(object sender, EventArgs e) { int cacheItems = HttpContext.Current.Cache.Count; Random rnd = new Random(); int intLessThanCacheCount = rnd.Next(cacheItems); \_lblCacheIndex.Text = intLessThanCacheCount.ToString(); \_lblCacheValue.Text = (string)HttpContext.Current.Cache\[intLessThanCacheCount.ToString()\]; lblSessionId.Text = HttpContext.Current.Session.SessionID; lblSessionValue.Text = ViewState\["Value"\].ToString(); }
---------------------------------------------------------- Lorem ipsum dolor sit amet.
Session ID will be different for different browsers, why did you expect to see the same? Session is recognized by user's cookies.
HttpContext.Current.Cache
is shared across application domain (MSDN[^]). If your IIS is configured to run as web farm (it can use not one process to serve requests) - cache object will be different for each process. In any other case it is the same.Die Energie der Welt ist konstant. Die Entropie der Welt strebt einem Maximum zu.
-
Session ID will be different for different browsers, why did you expect to see the same? Session is recognized by user's cookies.
HttpContext.Current.Cache
is shared across application domain (MSDN[^]). If your IIS is configured to run as web farm (it can use not one process to serve requests) - cache object will be different for each process. In any other case it is the same.Die Energie der Welt ist konstant. Die Entropie der Welt strebt einem Maximum zu.
No I didn't expect the Session ID to be the same for different browser programs. In fact I only expected the Session ID to be the same on the same browser instance (not even for the same browser program but different instances) between each postback but it's not. I half-expected the Session ID to be the same though in the same browser instance from different tabs.
---------------------------------------------------------- Lorem ipsum dolor sit amet.
modified on Monday, June 21, 2010 1:06 AM
-
I have a simple test web application with a single Default.aspx page with 2 buttons having the following handlers: What surprises me is I expected to get the same session ID between successive clicks of either buttons but I get different session ID's each time. I expected this to be different clicking from 2 different iexplorer instances and more so between an iexplorer and chrome / firefox. Also, from this experiment, it is obvious that HttpContext.Current.Cache is shared among clients as I get the values inserted from prior requests from different browsers.
protected void \_btnAdd\_Click(object sender, EventArgs e) { int cacheCount = HttpContext.Current.Cache.Count; Random rnd = new Random(); HttpContext.Current.Cache\[cacheCount.ToString()\] = TextBox1.Text; int intLessThanCacheCount = rnd.Next(cacheCount); \_lblCacheIndex.Text = intLessThanCacheCount.ToString(); \_lblCacheValue.Text = (string) HttpContext.Current.Cache\[intLessThanCacheCount.ToString()\]; lblSessionId.Text = HttpContext.Current.Session.SessionID; ViewState\["Value"\] = TextBox1.Text; } protected void \_btnGet\_Click(object sender, EventArgs e) { int cacheItems = HttpContext.Current.Cache.Count; Random rnd = new Random(); int intLessThanCacheCount = rnd.Next(cacheItems); \_lblCacheIndex.Text = intLessThanCacheCount.ToString(); \_lblCacheValue.Text = (string)HttpContext.Current.Cache\[intLessThanCacheCount.ToString()\]; lblSessionId.Text = HttpContext.Current.Session.SessionID; lblSessionValue.Text = ViewState\["Value"\].ToString(); }
---------------------------------------------------------- Lorem ipsum dolor sit amet.
If you're not actually storing anything in session, the server won't bother persisting it. So you'll get a different sessionid each time.