Reputation:
I have an web app in C# and after 20 mins the session times out. I can't change this because other apps use the same config.
So I am checking to see if the Session is NULL and if it is. It redirect and adds a QueryString with the Session ID. The problem I am having is when the Session times out the SessionID because NULL. Is there anyway to still get the sessionid after the timeout?
Thanks!
Upvotes: 0
Views: 1169
Reputation: 124726
A robust application won't care about Session expiry. This can happen before the Session timeout expires, for example if the application pool is recycled.
So a robust application will always test if a data item exists in Session, and refresh it (e.g. from a database) if not. E.g.:
MyType GetMyValue()
{
object o = Session["MyValue"];
if (o == null)
{
o = GetMyValueFromPersistentStore(...); // ... e.g. from a database
Session["MyValue"] = o;
}
return (MyType) o;
}
If you are using Session to hold transient data that has not been persisted, then you probably will need to redirect to a home page, and make the user start again. To be avoided if at all possible.
Upvotes: 1
Reputation: 44931
No, once the session is gone, it is gone for good.
However, if you have control over the web pages, you can create a keep-alive mechanism in javascript by calling back to a simple ping method in an ashx page that implements IRequiresSessionState. Will update with an example momentarily.
Here's the javascript that we use (note that this warns the user about the timeout, but it could easily just perform a non-posting query):
var m_TimeoutWarningID = null;
function ResetSessionTimeoutWarning() {
try {
// Clear the previous timeout warning, if any
if (m_TimeoutWarningID != null) {
window.clearTimeout(m_TimeoutWarningID);
}
// If the caller has set the session expiration minutes, and it is not 0, initialize the timer
if ((typeof m_wSessionTimeoutWarningMinutes !== 'undefined') && (m_wSessionTimeoutWarningMinutes != 0)) {
// Pass the expiration minutes to the called method in case something happens to the var between
// now and the time the method is executed.
m_TimeoutWarningID = window.setTimeout("SessionTimeoutWarning(" + m_wSessionTimeoutWarningMinutes + ")", m_wSessionExpiresInMinutes * 60000);
}
} catch (ex) {
alert('Javascript Error (ResetSessionTimeoutWarning)\r' + ex.message);
}
}
function SessionTimeoutWarning(wSessionTimeoutWarningMinutes) {
if (window) {
window.focus();
}
// Don't worry about exceptions here
if (window) {
window.focus();
}
if (confirm("Your login session will expire in " + wSessionTimeoutWarningMinutes + " minutes. Please press Ok within this time period to confirm you are still working.")) {
// Send a request back to the server to renew the session timeout
ResetSessionTimeout();
// Reset the session timeout warning now
ResetSessionTimeoutWarning();
}
}
In the above example, m_wSessionTimeoutWarningMinutes and m_wSessionExpiresInMinutes are set in server side code along with an initial call to ResetSessionTimeoutWarning().
ResestSessionTimeout performs an ajax request against an ASHX page that implements IRequiresSessionState just to keep the user's session alive.
Upvotes: 1
Reputation: 6054
When you are creating your SessionID, you can also save it to a browser cookie.
Then when you are checking to see if you have a SessionID, you check your session 1st. If it doesn't exist, check the cookie & set it back to a session variable.
Upvotes: 0