Solved

Session_OnEnd Event Not Firing

Posted on 2014-02-25
7
323 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now