Solved

Session_OnEnd Event Not Firing

Posted on 2014-02-25
7
326 Views
Last Modified: 2014-02-26
We currently have a web app that times out after 20 minutes as intended. When this occurs I want to display a message box telling the user that the session has timed and out an to click OK to refresh the page.

I have the following VB code on the back end:

Imports System.Windows.Forms

Partial Class _Default
    Inherits System.Web.UI.Page
    Public Event OnEnd As EventHandler
    Public Property Timeout As Integer

    Public Sub Session_OnEnd()
        MessageBox.Show("Your session has timed out. Click OK to refresh your page.", _
            "Important:", _
             MessageBoxButtons.OK, _
             MessageBoxIcon.Exclamation, _
             MessageBoxDefaultButton.Button1)

        Dim result As Integer = MessageBox.Show("message", "caption", MessageBoxButtons.OK)
        If result = DialogResult.OK Then
            Response.Redirect("default.aspx")
        End If
    End Sub
End Class

And for testing purposes I’ve set the following entries in the web.config file to 1 minute for testing:

<sessionState mode="InProc" cookieless="false" timeout="1"/>

<authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="1"/>


The problem is that the event doesn’t seem to fire. I’m going into debug, executing one search and leaving the page alone, the nothing is happening. What am I missing here?

Thanks,

JB
0
Comment
Question by:JB4375
  • 3
  • 2
  • 2
7 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 39886151
0
 
LVL 1

Author Comment

by:JB4375
ID: 39886458
Interesting methods, but they seem a little over the top for what I'm looking for.

Also, from the code project link:
Note: In all the above scenarios, I am assuming SetTimeout method and session related variables will be reset whenever there is a post back. This may not work 100%, when there could be partial rendering and the SetTimeout method and session related variables may not be reset.

Isn't there a dependable method of simply capturing the event when it occurs, and then displaying a message box etc. as I've described above?
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 39887327
The browser is independent of the server, and all communication between the two must be initiated by the browser. Hence, you cannot push a message box to the browser from the server. What I have done in the past is to add a <meta http-equiv="refresh"> tag to my page where the timeout value is set to be just a tad sooner than the actual session timeout value. This would perform an automatic refresh of the page. For your case, you wouldn't want this tag, but rather some Javascript to alert the user to the pending timeout. You could try doing something like (in your markup file):

<script>
    setTimeout(alertUserToTimeout, <%= Session.Timeout * 60000 %>);  <%-- Timeout is in minutes, so multiply by 60 for seconds, and by 1000 for milliseconds, which is what setTimeout expects --%>

    function alertUserToTimeout() {
        alert('Your session is about to expire!');
    }
</script>

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:JB4375
ID: 39887698
Right... I'm not concerned with alerting them before the session expires,

Just letting them know  when it has timed out so they aren't attempting to click around on the page before getting frustrated and then closing the window because they think something it wrong with the program.

I want the message box to inform them what has occurred, and clicking OK refreshes the page.

Questions:

In the code above, is session.timeout a system variable or can I just drop a number in there?

What would it look like for 20 minutes?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39887877
Session.Timeout is on the server, yes. It's value will be whatever you have configured it to be, the default being 20 minutes.

Yes, you can drop a number in there, but it would be better to use the variable so that the two values are in sync.

20 minutes would be: 20 x 60 x 1000 = 1200000
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39888351
If you want to push the message from the session_end event of the server to the client browser, you can use SignalR.

http://www.codeproject.com/Articles/526876/AddingplusSignalRplustoplusanplusASP-NetplusWebFor
0
 
LVL 1

Author Closing Comment

by:JB4375
ID: 39888843
Thanks for the help!!

Much appreciated as always,

JB
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question