TechNet
Products
IT Resources
Downloads
Training
Support
Products
Windows
Windows Server
System Center
Microsoft Edge
Office
Office 365
Exchange Server
SQL Server
SharePoint Products
Skype for Business
See all products »
Resources
Channel 9 Video
Evaluation Center
Learning Resources
Microsoft Tech Companion App
Microsoft Technical Communities
Microsoft Virtual Academy
Script Center
Server and Tools Blogs
TechNet Blogs
TechNet Flash Newsletter
TechNet Gallery
TechNet Library
TechNet Magazine
TechNet Wiki
Windows Sysinternals
Virtual Labs
Solutions
Networking
Cloud and Datacenter
Security
Virtualization
Updates
Service Packs
Security Bulletins
Windows Update
Trials
Windows Server 2016
System Center 2016
Windows 10 Enterprise
SQL Server 2016
See all trials »
Related Sites
Microsoft Download Center
Microsoft Evaluation Center
Drivers
Windows Sysinternals
TechNet Gallery
Training
Expert-led, virtual classes
Training Catalog
Class Locator
Microsoft Virtual Academy
Free Windows Server 2012 courses
Free Windows 8 courses
SQL Server training
Microsoft Official Courses On-Demand
Certifications
Certification overview
Special offers
MCSE Cloud Platform and Infrastructure
MCSE: Mobility
MCSE: Data Management and Analytics
MCSE Productivity
Other resources
Microsoft Events
Exam Replay
Born To Learn blog
Find technical communities in your area
Azure training
Official Practice Tests
Support options
For business
For developers
For IT professionals
For technical support
Support offerings
More support
Microsoft Premier Online
TechNet Forums
MSDN Forums
Security Bulletins & Advisories
Not an IT pro?
Microsoft Customer Support
Microsoft Community Forums
Sign in
Home
Library
Wiki
Learn
Gallery
Downloads
Support
Forums
Blogs
Resources For IT Professionals
United States (English)
Россия (Pусский)
中国(简体中文)
Brasil (Português)
Skip to locale bar
Post an article
Translate this page
Powered by
Microsoft® Translator
Wikis - Page Details
First published by
Mohammad Nizamuddin
When:
9 Feb 2013 1:04 PM
Last revision by
Ken Cenerelli
(MVP, Microsoft Community Contributor)
When:
9 Apr 2016 8:02 PM
Revisions:
4
Comments:
2
Options
Subscribe to Article (RSS)
Share this
Engage!
Wiki Ninjas Blog
(
Announcements
)
Wiki Ninjas on Twitter
TechNet Wiki Discussion Forum
Can You Improve This Article?
Positively!
Click Sign In to add the tip, solution, correction or comment that will help other users.
Report inappropriate content using
these instructions
.
Wiki
>
TechNet Articles
>
Keep session alive in asp.net application
Keep session alive in asp.net application
Article
History
Keep session alive in asp.net application
Friends, many times we get requirements to increase the session timeout expiry time on the application server, so what we do basically we go in web.config file and set session time out to some time period, by default its 20 minutes, but remember sometime you increase time period in web.config file and it doesn't work. Why? The reason is you also have to check your application pool recycling time period it should be more than the session expiry period which you are doing in web.config file.
Yo! But anyways you did not avoid session expiration in your application, session will still expire in your application. Let's say if user filling out a long form in your application or kept a page open and gone out for coffee. So now what will happen? Session will expire right by the time use will be back and resume with the work.
Some people do session resetting in code behind by checking if session is null then read User ID () from cookies and reset it. Or if it is intranet application and AD authentication then you get the current logged in user in system and set it in session.
Now if you don't wanna do all above crap in your application, what all you wanna do is don't let session to get expired unless user closes the browser.
To implement this behavior in your applications simply follow below steps:
First of all here is some brief info about working session state in ASP.NET. By default it is set to 20 minutes, so if user sends a request to the server (in simple language, browse a page) then server stores the time when request came and extends the session time out to 20 minutes.If after 10 minutes user sends again a request it keeps extended to 20 minutes. If user doesn't send any request to server til 20 minutes then session get expired. This is basic working of session state.
From first step you know that server need to get any request from client to keep session alive. So now we can think of some methodology to keep pinging the server. Below is the script which will keep sending request to the server and will keep session alive.
Create a
SessionCheck.aspx
page in your application. You can keep this page blank so that if it gets called from the browser then it should not be over loaded.
Using Java Script:
Image will be used to keep session alive by changing image src, assigning this property to some.
<
img
id
="imgSessionCheck"
width
="1"
height
="1"
/>
<
script
type
="text/javascript"
>
//Variable used to prevent caching on some browsers and knowing the count how many times user sends request to server.
var
counter;
counter = 0;
function
KeepSession() {
// Increase counter value
counter++;
// Gets reference of image
var
img = document.getElementById(
"imgSessionCheck"
);
// Set new src value, which will send a request to the server
img.src =
"http://servername:port/appName/SessionCheck.aspx?count="
+ counter;
// now schedule this process to happen in some time interval, in this example its 1 min
setTimeout(KeepSession, 60000);
}
// Call this function for a first time
KeepSession();
</
script
>
Using JQuery:
<
script
language
="javascript"
type
="text/javascript"
src
="http://code.jquery.com/jquery-latest.js"></
script
>
<
script
language
="javascript"
type
="text/javascript">
function
KeepSession() {
// A request to server
$.post(
"http://servername:port/appName/SessionCheck.aspx"
);
//
now schedule this process to happen in some time interval, in this example its 1 min
setInterval(KeepSession, 60000);
}
// First time call of function
KeepSession();
</
script
>
So it's simple. You just need to put this script in your page.