I have tried very hard but cannot find a solution on how to set session timeout value for in-process session for an ASP.Net web application.
I am using VSTS 2008 + .Net 3.5 + C#. Here is what I wrote by myself to set timeout to be 1 minute, is it correct?
I wrote under system.web section in the web.config
<sessionState timeout="1" mode="InProc" />
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
If you want to set the timeout to 20 minutes, use something like this:
<configuration>
<system.web>
<sessionState timeout="20"></sessionState>
</system.web>
</configuration>
Method 2
The value you are setting in the timeout
attribute is the one of the correct ways to set the session timeout value.
The timeout
attribute specifies the number of minutes a session can be idle before it is abandoned. The default value for this attribute is 20.
By assigning a value of 1 to this attribute, you’ve set the session to be abandoned in 1 minute after its idle.
To test this, create a simple aspx page, and write this code in the Page_Load event,
Response.Write(Session.SessionID);
Open a browser and go to this page. A session id will be printed. Wait for a minute to pass, then hit refresh. The session id will change.
Now, if my guess is correct, you want to make your users log out as soon as the session times out. For doing this, you can rig up a login page which will verify the user credentials, and create a session variable like this –
Session["UserId"] = 1;
Now, you will have to perform a check on every page for this variable like this –
if(Session["UserId"] == null) Response.Redirect("login.aspx");
This is a bare-bones example of how this will work.
But, for making your production quality secure apps, use Roles & Membership classes provided by ASP.NET. They provide Forms-based authentication which is much more reliabletha the normal Session-based authentication you are trying to use.
Method 3
If you are using MVC, you put this in the web.config file in the Root directory of the web application, not the web.config in the Views directory. It also needs to be IN the system.web node, not under like George2 stated in his question: “I wrote under system.web section in the web.config”
The timeout parameter value represents minutes.
There are other attributes that can be set in the sessionState element. You can find information here: docs.microsoft.com sessionState
<configuration> <system.web> <sessionState timeout="20"></sessionState> </system.web> </configuration>
You can then catch the begining of a new session in the Global.asax file by adding the following method:
void Session_Start(object sender, EventArgs e) { if (Session.IsNewSession) { //do things that need to happen //when a new session starts. } }
Method 4
Use this in web.config
:
<sessionState timeout="20" />
Method 5
If it’s not working from web.config
, you need to set it from IIS.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0