About the author

Vijay Kodali
E-mail me Send mail

Site Statistics

Site Meter

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Alert user before session timeout

Here is a quick & dirty trick to alert users on session timeout. This is not a perfect solution. But it will give reader an idea to work on...

Some facts before we start:

Session doesn't end

  • When the user closes his browser
  • When the user navigates away from your page
  • When user connection lost.

Session ends, when the server hasn't gotten a request from the user in a specific time (Session timeout value).

In this solution, I am using ajaxtoolkit’s modalpopupextender control to alert user about expiring session.

Each time a page is rendered back to the client, I am injecting JavaScript that will show modalpopup two minutes before session timeout. I am passing the session expiry value to the client side java script. This will execute a countdown, and at the end display the Popup.

I added modalpopupextender to the page and set its target control id to a panel. That panel contains alert message and two buttons.

clip_image002

The Page_Load code looks like this.

clip_image004

I added two java script functions, one for showing alert message and second one is for hiding that message.

Here are javascript functions

clip_image006

That’s it. Run the application and it will check 2 minutes before the timeout and provide user the option to "slide" the session. If user clicks “OK” it will refresh page, which in turn will slide the session. If user clicks “Cancel” the popup will hide.

clip_image008

 

You can improve this code on each step. Like for example, to renew session you don’t have to refresh the page. You can just call web service from client side etc.

If you have any questions, leave a comment.


Posted by vijay on Thursday, May 20, 2010 8:51 PM
Permalink | Comments (16) | Post RSSRSS comment feed

Comments

Vijay Kodali's Blog

Thursday, May 20, 2010 9:22 PM

trackback

Alert user before session timeout

Note: Cross posted from Vijay Kodali's Blog . Permalink Here is a quick & dirty trick to alert users

Community Blogs

Friday, May 21, 2010 8:14 AM

trackback

Alert user when their session is about to expire

Note: Cross posted from Vijay Kodali's Blog . Permalink Here is a quick and dirty trick to alert

AndyT United States

Friday, May 21, 2010 9:02 AM

AndyT

This is fine if you're using a homegrown, session-based login, but it's a bit more complicated to get the amount of time until timeout when using ASP.NET forms based authentication and the membership provider.  Specifically, the sliding timeout causes the timeout duration to vary on postback, and you need to check the FormsAuthenticationTicket for the amount of seconds remaining in the current window.

Something like this will return the actual amount of seconds until the forms authentication ticket expires:
var authTicket = System.Web.Security.FormsAuthentication.Decrypt(Request.Cookies[System.Web.Security.FormsAuthentication.FormsCookieName].Value);
var secondsToTimeout = Convert.ToInt32(authTicket.Expiration.Subtract(DateTime.Now).TotalSeconds);

Then to warn/redirect at 2 minutes and 1 minute prior to timeout:
var primaryWarningSeconds = (timeoutSeconds - 120);
var secondaryWarningSEconds = (timeoutSeconds - 60);

Smile

Vimal India

Monday, June 21, 2010 4:27 AM

Vimal

Hi
Thanks for this article..but i have a single question..

Let say i am implementing this code in my application which is of two individual forms and i am opening two different forms in two different tabs(browser tab) will it work properly? I think no because it will show two different pop-up messages for two different forms.

Is there any way to solve this?

Thanks
Vimal

reza Canada

Monday, July 19, 2010 11:46 AM

reza

It was great.

Priscila Dagley United Kingdom

Sunday, August 29, 2010 11:38 AM

Priscila Dagley

This is without doubt a great piece of writing - well focused, but i did have a problem in getting this comment accepted

Chetan India

Monday, August 30, 2010 9:22 AM

Chetan

What if user opens the screen in new tab. For previous tab it shows message. Is there any alternate way?

Ian Berends United States

Tuesday, August 31, 2010 1:17 PM

Ian Berends

Many bloggers are great leader; especially lead their own blog to become the most popular blog in the world. I wonder what you think about this. Many successful bloggers created or made jobs for other people. It is wise idea, I think. We agree that you can become this too. By commenting on your blog, I trust you because I had “blog-walked” to your blog intensively, especially see and check your analysis. It is fantastic! Wonderful thought that you gave to other readers, share the biggest idea, unique thought, and then debate them. For me as learner, it is the most important thing to you and us. I have spent many times, from time to come, and I am sure this is the right time to me to recruit you as a writer, hope you are responsible to your future job. Great time to comment on this page, I am looking forward to chatting or email with you

utah attorney United States

Thursday, September 02, 2010 7:00 AM

utah attorney

I like this trick and going to use it.I hope see more tricks by you soon.You are doing great work keep it up man.

http://unitedheartsforhealth.org United States

Thursday, September 02, 2010 10:21 AM

http://unitedheartsforhealth.org

It is really interesting how you write things out, but a good read either way. Much love from your number 1 fan , Eric

tinnitus remedies United States

Thursday, September 02, 2010 10:21 PM

tinnitus remedies

Excellent blog post, a bunch of really good knowledge.  I am about to show my friend and ask them the things they think.

Doctor C.J. Mason United States

Friday, September 03, 2010 4:30 AM

Doctor C.J. Mason

Your personal message emailed to 500,000 Unique Prospects. All interested in your service or product. These are not just your regular everyday emails. The list members chose to join it by double-opting in. Most people have found that the list is highly responsive. What more could you ask for? A one time emailing to 500,000 Basic Unique Prospects is just $9.95. Order Now For A Boost Of Unique Prospects To 1,000,000. We feel It will please you to know that we have been an internationally recognized company for over ten years. You better hope you visit our website before your competition does!

http://poststream.org United States

Monday, September 06, 2010 11:03 AM

http://poststream.org

Long time reader, first time commenter Smile You're writing style is great; I am an aspiring writer myself! In college at the moment. Talk to you soon, Mary

Funeral Plan United States

Tuesday, September 07, 2010 8:49 AM

Funeral Plan

if you're using a homegrown, session-based login, but it's a bit more complicated to get the amount of time until timeout when using ASP.NET forms based authentication and the membership provider.  Specifically, the sliding timeout causes the timeout duration to vary on postback, and you need to check the FormsAuthenticationTicket for the amount of seconds remaining in the current window.

Reid Monteforte United States

Tuesday, September 07, 2010 4:27 PM

Reid Monteforte

    Why is it that we here in America are the frist group the world thinks should back down from a disagreement with anybody? The rest of the world hates us and always so go get used to it. They burn our christians and bibles and our flag and yet we are told to always turn the other cheek so to speak.

slimming undergarments United States

Tuesday, September 07, 2010 4:55 PM

slimming undergarments

This is an amazing blog site.  I found it whilst I was attempting to fix a different issue.  Your internet site is definitely worth the save and i'm sure it'll be useful in the foreseeable future.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading