16
votes

I have an Asp.net Mvc application with razor views engine , in which i used a variable Session['user'] : when an user log on the application Session['user'] = login and in the logout this variable takes as value Null.

The problem is that there is a short timeout and the session variable expires : if i do nothing in one minute after log on the application Session['user'] =null automatically.

So how can i set the timeout of the session's variable unlimited until it is changed by program?Any suggestions?

5

5 Answers

31
votes

So how can i set the timeout of the session's variable unlimited until it is changed by program?Any suggestions?

You can't set timeout value to unlimited.

You can increase the time out value in minutes using the timeout attribute of sessionState element in web.config.


SESSION STATE SETTINGS

By default ASP.NET uses cookies to identify which requests belong to a particular session. If cookies are not available, a session can be tracked by adding a session identifier to the URL. To disable cookies, set sessionState cookieless="true". (120 = minutes)

<sessionState mode="StateServer" cookieless="false" timeout="120"/>

Check out this Session-Time out

14
votes

You cannot assign it to unlimited. You can increase the value in minutes using the time out attribute of Session state element in web.config

<sessionState timeout="30">
</sessionState>

By default session timeout value is 20 minutes. Also in your case if you are using forms authentication, check the authentication time out value as well

<authentication mode="Forms">
   <forms loginUrl="logon.aspx" 
   protection="All" path="/" timeout="30" />
</authentication>  
8
votes

It's timeout of the session, not the variable. Set it in configuration in minutes

<sessionState timeout="30" />
8
votes

It is not possible to set the session time out to unlimited.
Instead set the session time out to a high value.
Example:

<configuration>
  <system.web>
    <sessionState mode="InProc" timeout="350" />
  </system.web>
</configuration>
1
votes

I had a similar problem earlier and it was not about the Session Timeout value. Hence, Sometimes its now about the Timeout period. This Session Timeout may be set to 20 mins or more but if the hosting server or your computer is VERY LOW on MEMORY. The Session values are then cleared and the user will have to sometimes login again.

This low memory is sometimes caused by STORING IMAGES or BINARY VALUES in the Database instead of storing as Files on the Server and access them using System.IO.File procedure. So when you attempt to fetch some records, their IMAGES DATA on the TABLE will also be fetched, resulting in a SIGNIFICANT reduction in performance and also OVER-CONSUMPTION of available MEMORY.

IT MAY BE THAT YOUR COMPUTER OR HOSTING IS VERY LOW ON MEMORY (Not Storage Space)

Hope this helps.