About the author

Vijay Kodali
E-mail me Send mail

Recent comments

Authors

Disclaimer

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

© Copyright 2017

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 Mílahaŋska Tȟamákȟočhe

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 རྒྱ་གར་

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.

Govin சிங்கப்பூர்

Wednesday, November 3, 2010 3:59 AM

Govin

Hi Nice example , but never work for me...

where will put first code in my asp file and no pop up....



please its very urgent help me

wildo Mílahaŋska Tȟamákȟočhe

Monday, November 22, 2010 3:26 AM

wildo

please post your full code

brinda Mílahaŋska Tȟamákȟočhe

Tuesday, December 7, 2010 7:30 PM

brinda

Hi...
Two button OK and Cancel within panel doesnot work for me.
It want that when I click OK it should extend session for me and when i click cancel it should log out to the admin login page.

<cc1:ModalPopupExtender ID="mpe_alert" runat="server"  TargetControlID="pnlAlert"  CancelControlID="btnCancel" OKControlID="btnOK"
           PopupControlID="pnlAlert" BehaviorID="mpe_alert">
       </cc1:ModalPopupExtender>

       <aspTonganel style="display: none" ID="pnlAlert" runat="server">
     <table  style="height:100px;background-color:#ffffff;" cellspacing="0" cellpadding="0" width="340" border="0" >
       <tbody>
       <tr>
                <td  style="width: 200px; color:#ffffff; height:20px; background-color:#003366">

                                        Security Warning
                </td>
       </tr>
       <tr>
                <td>
                </td>
       </tr>
       <tr align="center">
                <td>
                    Your login session is about to expire.Do you want to extend it?
                </td>
       </tr>
       <tr>
                <td>
                    <table cellpadding="0" cellspacing="0" width="100%" border="0">
                    
                     <tr align="center">
                        <td>
                        <a href="../AdminLogin.aspx" >Logout</a>
                        <asp:Button ID="btnOK" runat="server" Text="Continue" OnClick="btnContinue_Click" />
                        <asp:Button ID="btnCancel" runat="server" Text="Logout" OnClick="btnCancel_Click"/>
                        </td>
                    
                     </tr>
                    
                    </table>
                
                
                </td>
      
       </tr>
      
       </tbody>

       </table>
       </aspTonganel>
Now in code behind file
In button_click event I have following code

protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("AdminLogin.aspx");
    
    }
protected void btnOK_Click((object sender, EventArgs e)
{
//I want here code to extend session
}

Problem is that both of button does not get fired when I click it.I think it is issue with modal popup extender.Please anyone can help me I am trying since two days...
It is urgent for me.
Thanks in advance

Deepa Mílahaŋska Tȟamákȟočhe

Monday, September 26, 2011 1:55 PM

Deepa

Hi I have a question.Your code works fine and display messages 2 min before timeout and if I don't click anything after the prompt comes, it take me back to login page.But I don't get one point. I have set the seesion time out for 3 minutes for testing purpose. So 1 min after i login i get prompt and when I click OK as per my understanding, code will retain session for next 3 minutes and teh code will prompt for the message again. BUt I am not getting that prompt after I click OK.Please tell me what I did wrong

Deepa Mílahaŋska Tȟamákȟočhe

Monday, September 26, 2011 1:56 PM

Deepa

Hi I have a question.Your code works fine and display messages 2 min before timeout and if I don't click anything after the prompt comes, it take me back to login page.But I don't get one point. I have set the seesion time out for 3 minutes for testing purpose. So 1 min after i login i get prompt and when I click OK as per my understanding, code will retain session for next 3 minutes and teh code will prompt for the message again. BUt I am not getting that prompt after I click OK.Please tell me what I did wrong

vijay Mílahaŋska Tȟamákȟočhe

Saturday, October 1, 2011 8:31 PM

vijay

@Deepa, clicking "OK" should reset session timeout value. Make sure page is refreshing correctly.

Deepa Mílahaŋska Tȟamákȟočhe

Sunday, October 2, 2011 6:57 AM

Deepa

Hi the reason was that i was using updatepanel, and I have placed the code in not postback loop. Changing that resolved my issue. But I have another problem. When I click OK the page gets refreshed, thus extends my session time. But the popup message not evn checking idle time. Means I have set the session timeout to 3 minutes for testing purpose. So whatever post back I do in each 3 minutes I am getting this popup. What I need is when user becomes idle for certain time show this message

Bala རྒྱ་གར་

Saturday, November 5, 2011 1:43 AM

Bala

Very Nice yaar.............
thanks..

dotnetstockage.wordpress.com

Monday, February 6, 2012 4:22 AM

pingback

Pingback from dotnetstockage.wordpress.com

Usefull links for asp.net  « dotnetstockage

nuthan རྒྱ་གར་

Tuesday, February 28, 2012 3:01 AM

nuthan

How Can I Call WebService to create new session could you please provide sample snippet for that.
thanks in advance.

www.csharpaspnetarticles.com རྒྱ་གར་

Friday, April 27, 2012 5:02 AM

www.csharpaspnetarticles.com

This code redirects after a fix time irrespective of inactive session , where as session doesn't timeouts is use is active on site

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading