Session cleanup in Global Application_Error

Whenever an unhandled exception occurs on our site, I want:

  • Send email notification
  • Clear user session
  • Send the user to an error page ("Sorry, there was a problem ...")

The first and last one I worked for a long time and the second one was giving me some problems. My Global.asax.vb includes:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    ' Send exception report
    Dim ex As System.Exception = Nothing
    If HttpContext.Current IsNot Nothing AndAlso HttpContext.Current.Server IsNot Nothing Then
        ex = HttpContext.Current.Server.GetLastError
    End If
    Dim eh As New ErrorHandling(ex)
    eh.SendError()

    ' Clear session
    If HttpContext.Current IsNot Nothing AndAlso HttpContext.Current.Session IsNot Nothing Then
        HttpContext.Current.Session.Clear()
    End If

    ' User will now be sent to the 500 error page (by the CustomError setting in web.config)
End Sub

      

When I start debugging I see that the session is cleared, but then on the next page the session comes back again!

I eventually found a link that says that changes to the session won't be saved unless Server.ClearError is thrown. Unfortunately, if I add this (just below the line that sets "ex"), the CustomErrors redirection doesn't seem to work and I'm left with a blank page?

Is there a way to get around this?

+2


a source to share


2 answers


Is it possible to clear the session on the custom error page?



Additionally, you may need to learn ELMAH. It makes everything you're trying to do easy and simple.

0


a source


Brunis. In response to your question (I had the same problem) your application may have crashed on application_error at some point before you can access session information such as the Begin_Request stage.



+1


a source







All Articles