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
(eMicrosoft Community Contributor, Microsoft Partne)
When:
9 Feb 2013 1:04 PM
Last revision by
Maheshkumar S Tiwari
When:
19 Sep 2013 9:03 AM
Revisions:
2
Comments:
1
Options
Subscribe to Article (RSS)
Share this
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 mins, 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 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 expire unless user close the browser. To implement this behavior in your applications simply follow below steps.
First of all let me give you some brief about working session state in asp.net. By default it is set to 20 min, so if user sends a request to the server (in simple language browse a page) then server stores the time when request came and extend the session time out to 20 min, so if after 10 min user send again a request so it keeps extended to 20 min, if user don't send any request to server till 20 min then session get expires. 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 Scrip:
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 just you need to put this script in your page.
ASP.NET
,
en-US
,
has code
,
Javascript
[Edit tags]
Leave a Comment
Please add 5 and 7 and type the answer here:
Post
Wiki - Revision Comment List(Revision Comment)
Sort by:
Published Date
|
Most Recent
|
Most Useful
Comments
Maheshkumar S Tiwari
19 Sep 2013 9:03 AM
Maheshkumar S Tiwari edited Original. Comment: Added tags
Edit
Page 1 of 1 (1 items)
Wikis - Comment List
Sort by:
Published Date
|
Most Recent
|
Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
Posted by
Maheshkumar S Tiwari
on
19 Sep 2013 9:03 AM
Maheshkumar S Tiwari edited Original. Comment: Added tags
Edit
Page 1 of 1 (1 items)