Session Statehttp://forums.asp.net/t/1240004.aspx/1?Session+StateTue, 24 Jun 2008 06:06:17 -040012400042262405http://forums.asp.net/p/1240004/2262405.aspx/1?Session+StateSession State <p>I am new to the whole MVC approach to web development.</p> <p>How do you keep session state for a user, on the server, between http posts.</p> <p>Examples: Login/Logout, Shopping cart content, etc.</p> <p>&nbsp;</p> 2008-03-28T16:14:27-04:002262550http://forums.asp.net/p/1240004/2262550.aspx/1?Re+Session+StateRe: Session State <p>in controller use session<br> </p> <p><b>&nbsp;HttpSessionStateBase session = HttpContext.Session;</b></p> <p>&nbsp;</p> <p>i have ex:</p> <p><b>create login controller&nbsp;</b></p> <p>&nbsp;public void Login(string id){</p> <p>&nbsp;HttpSessionStateBase session = HttpContext.Session;</p> <p>// check username , pass . </p> <p>if(checkuser(){</p> <p>&nbsp;&nbsp;&nbsp; session[&quot;login&quot;] = true ; </p> <p>....&nbsp;</p> <p>}&nbsp;</p> <p><b>create check login</b> </p> <p>public bool CheckLogin()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpSessionStateBase session = HttpContext.Session;<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (session[&quot;Login&quot;] == null)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (bool)session[&quot;Login&quot;];<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>} <br> </p> <p>... <br> </p> <p>:)&nbsp;</p> <p><br> </p> 2008-03-28T17:19:06-04:002262906http://forums.asp.net/p/1240004/2262906.aspx/1?Re+Session+StateRe: Session State <p>Is this session lifetime controlled the same as forms based asp.net apps.</p> <p>I tried setting the timeout to 1, but the state seemed to persist beyond 1 minute.</p> <p>Also, I couldn't find an event to catch or hook to know when the session has timed out. (i.e. to allow for a redirect)</p> <p>&nbsp;Thanks</p> 2008-03-28T20:14:21-04:002263278http://forums.asp.net/p/1240004/2263278.aspx/1?Re+Session+StateRe: Session State <p>http://msdn2.microsoft.com/en-us/library/ms972429.aspx</p> <p><font face="Verdana" size="2">The ASP.NET HttpSessionState class provides a useful IsNewSession( ) method that returns true if a new session was created for this request.&nbsp; The key to detecting a session timeout is to also look for the ASP.NET_SessionId cookie in the request.&nbsp; If this is a new session but the cookie is present, this indicates a timeout situation.&nbsp; In order to implement this effectively for an entire web site, it is useful to utilize the Base Page concept described in a</font></p> <p>&nbsp;</p> <p>http://aspalliance.com/520_Detecting_ASPNET_Session_Timeouts.2</p> <p>&nbsp;</p> <p>http://www.webmonkey.com/webmonkey/03/30/index3a.html?tw=programming</p> <p>&nbsp;</p> <p>Fun.&nbsp;</p> 2008-03-29T02:50:37-04:002300180http://forums.asp.net/p/1240004/2300180.aspx/1?Re+Session+StateRe: Session State <p>what assemblies do you have to include for this code to work? I tried:</p> <p><font color="#0000ff" size="2">using</font><font size="2"> System.Web;</p> </font><font color="#0000ff" size="2"> <p>using</font><font size="2"> System.Web.SessionState;</font></p> <p><font size="2">without success.</font></p> <p><font size="2"></font><font size="2">(edit: Error&nbsp;363&nbsp;An object reference is required for the non-static field, method, or property 'System.Web.HttpContext.Session.get'&nbsp;myproject\Controllers\HomeController.cs&nbsp;...</p> </font> 2008-04-15T23:43:04-04:002301257http://forums.asp.net/p/1240004/2301257.aspx/1?Re+Session+StateRe: Session State <p>Have you tried HttpContext.Current.Session ?</p> 2008-04-16T11:27:53-04:002301490http://forums.asp.net/p/1240004/2301490.aspx/1?Re+Session+StateRe: Session State <p></p> <blockquote><span class="icon-blockquote"></span> <h4>ykng</h4> <p>Have you tried HttpContext.Current.Session ?</p> <p></p> </blockquote> <p></p> <p>Do not use this - it is not possible to mock it in tests.</p> <p>You usually need to write something like this in your Controller tests:</p> <b><font size="2"></font></b><b><font size="2"></font><font size="2">&nbsp;&nbsp;&nbsp;HttpContextBase httpContext = (HttpContextBase)_mocks.CreateMock(typeof(HttpContextBase));<br> &nbsp;&nbsp;&nbsp;HttpSessionStateBase session = (HttpSessionStateBase)_mocks.CreateMock(typeof(HttpSessionStateBase));<br> &nbsp;&nbsp;&nbsp;ControllerContext cc = new ControllerContext(httpContext, new RouteData(), c);<br> &nbsp;&nbsp; yourController.ControllerContext = cc;<br> </font><font size="2"></font><font size="2"> <p><br> </p> </font></b> 2008-04-16T13:17:24-04:002442113http://forums.asp.net/p/1240004/2442113.aspx/1?Re+Session+StateRe: Session State <p>Hi All</p> <p>I am working an web application in asp.net 2.0</p> <p>when i am opening the page in one system is working fine</p> <p>at the same time u open the same page in another system, the values in the variables of the page are changing according to the changes done in another system if i made changes then the changed values r reflected in another system too&nbsp;</p> <p>might be both are working&nbsp; on same session even though they r on two different system</p> can any one can&nbsp; sort this out <p>Thanks in Advance <br> </p> <p>&nbsp;</p> 2008-06-24T06:06:17-04:00