How to end a session when the user closes the browser?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news.microsoft.com

    How to end a session when the user closes the browser?

    Hi everyone,

    I need some help (may be in the form of some sample code) for the subject
    question.

    I have an ASP.NET/C# application. I need to do quite a few tasks when the
    session ends. I don't want to depend on the session timeout factor since it
    may end too soon or it may hang around too long. I can't find any event
    that fires when the browser closes. The Session_End event handler in the
    Global.asax does not execute until the timeout value expires. Also, when it
    does execute the Session_end, it seems that the session has already ended
    and so, I have no access to any of the session variables I was holding onto.

    Can anybody tell me the server side sequence of events and their handlers
    that get called when the user closes a browser? Does the browser inform the
    server that it is closing, so that I can do my cleanup?

    Any help is highly appreciated.

    Babu.


  • Lionel LASKE

    #2
    Re: How to end a session when the user closes the browser?


    Unfortunatly, nothing happens on the server when the browser is closed.
    I'm afraid that you can't close the session without waiting the timeout and
    the "Session_En d" event.

    Lionel.


    "news.microsoft .com" <babupalladium1 @yahoo.com> a écrit dans le message de
    news: uBWhueDCFHA.468 @TK2MSFTNGP15.p hx.gbl...[color=blue]
    > Hi everyone,
    >
    > I need some help (may be in the form of some sample code) for the subject
    > question.
    >
    > I have an ASP.NET/C# application. I need to do quite a few tasks when the
    > session ends. I don't want to depend on the session timeout factor since
    > it may end too soon or it may hang around too long. I can't find any
    > event that fires when the browser closes. The Session_End event handler
    > in the Global.asax does not execute until the timeout value expires.
    > Also, when it does execute the Session_end, it seems that the session has
    > already ended and so, I have no access to any of the session variables I
    > was holding onto.
    >
    > Can anybody tell me the server side sequence of events and their handlers
    > that get called when the user closes a browser? Does the browser inform
    > the server that it is closing, so that I can do my cleanup?
    >
    > Any help is highly appreciated.
    >
    > Babu.
    >
    >[/color]


    Comment

    • news.microsoft.com

      #3
      Re: How to end a session when the user closes the browser?

      Thanks for the reply, Lionel. That was fast!

      OK, then, is there another event that fires before the call to the
      Session_End handler in the Global.asax? Because by the time the execution
      arrives at this handler, I have no access to any of the Session variables
      (It seems that the session has already been removed). If only I could find
      an event that fires with the session state intact just before the session
      closes.

      Babu.

      "Lionel LASKE" <llaske@c2s.f r> wrote in message
      news:esjkZjDCFH A.2392@TK2MSFTN GP14.phx.gbl...[color=blue]
      >
      > Unfortunatly, nothing happens on the server when the browser is closed.
      > I'm afraid that you can't close the session without waiting the timeout
      > and the "Session_En d" event.
      >
      > Lionel.
      >
      >
      > "news.microsoft .com" <babupalladium1 @yahoo.com> a écrit dans le message de
      > news: uBWhueDCFHA.468 @TK2MSFTNGP15.p hx.gbl...[color=green]
      >> Hi everyone,
      >>
      >> I need some help (may be in the form of some sample code) for the subject
      >> question.
      >>
      >> I have an ASP.NET/C# application. I need to do quite a few tasks when
      >> the session ends. I don't want to depend on the session timeout factor
      >> since it may end too soon or it may hang around too long. I can't find
      >> any event that fires when the browser closes. The Session_End event
      >> handler in the Global.asax does not execute until the timeout value
      >> expires. Also, when it does execute the Session_end, it seems that the
      >> session has already ended and so, I have no access to any of the session
      >> variables I was holding onto.
      >>
      >> Can anybody tell me the server side sequence of events and their handlers
      >> that get called when the user closes a browser? Does the browser inform
      >> the server that it is closing, so that I can do my cleanup?
      >>
      >> Any help is highly appreciated.
      >>
      >> Babu.
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Juan T. Llibre

        #4
        Re: How to end a session when the user closes the browser?

        re:[color=blue]
        > Unfortunatly, nothing happens on the server when the browser is closed.[/color]

        Right.

        re:[color=blue]
        > I'm afraid that you can't close the session without waiting the timeout
        > and the "Session_En d" event.[/color]

        You can programmaticall y call Session.abandon
        to force the end of a session at any time.

        That won't help Babu at all, but ending the session *can* be
        done without the need to wait for the timeout or Session_End.

        One way to persist session values, so they're restored the next
        time the visitor returns, would be to dump the session values into
        a cookie named with the SessionID. Each time you add a Session
        variable, you add a cookie key with the variable's value.

        When the user returns, reading the cookie would enable
        you to set the same values to the current session.

        Alternately, you could dump the session values into a db table,
        using the SessionID as the key. Then, reading a simple cookie
        set with a key name = SessionID would allow you to set the
        current session values to the previous session values.

        Why you'd want to do that beats me, but it's not too hard to do.



        Juan T. Llibre
        ASP.NET MVP
        ===========
        "Lionel LASKE" <llaske@c2s.f r> wrote in message
        news:esjkZjDCFH A.2392@TK2MSFTN GP14.phx.gbl...[color=blue]
        >
        > Unfortunatly, nothing happens on the server when the browser is closed.
        > I'm afraid that you can't close the session without waiting the timeout
        > and the "Session_En d" event.
        >
        > Lionel.
        >
        >
        > "news.microsoft .com" <babupalladium1 @yahoo.com> a écrit dans le message de
        > news: uBWhueDCFHA.468 @TK2MSFTNGP15.p hx.gbl...[color=green]
        >> Hi everyone,
        >>
        >> I need some help (may be in the form of some sample code) for the subject
        >> question.
        >>
        >> I have an ASP.NET/C# application. I need to do quite a few tasks when
        >> the session ends. I don't want to depend on the session timeout factor
        >> since it may end too soon or it may hang around too long. I can't find
        >> any event that fires when the browser closes. The Session_End event
        >> handler in the Global.asax does not execute until the timeout value
        >> expires. Also, when it does execute the Session_end, it seems that the
        >> session has already ended and so, I have no access to any of the session
        >> variables I was holding onto.
        >>
        >> Can anybody tell me the server side sequence of events and their handlers
        >> that get called when the user closes a browser? Does the browser inform
        >> the server that it is closing, so that I can do my cleanup?
        >>
        >> Any help is highly appreciated.
        >>
        >> Babu.
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Eliyahu Goldin

          #5
          Re: How to end a session when the user closes the browser?

          Babu,

          You do have some limited possibilities. You can use
          Response.IsClie ntConnected. Google for it to find out how it can help.

          Eliyahu

          "news.microsoft .com" <babupalladium1 @yahoo.com> wrote in message
          news:uBWhueDCFH A.468@TK2MSFTNG P15.phx.gbl...[color=blue]
          > Hi everyone,
          >
          > I need some help (may be in the form of some sample code) for the subject
          > question.
          >
          > I have an ASP.NET/C# application. I need to do quite a few tasks when the
          > session ends. I don't want to depend on the session timeout factor since[/color]
          it[color=blue]
          > may end too soon or it may hang around too long. I can't find any event
          > that fires when the browser closes. The Session_End event handler in the
          > Global.asax does not execute until the timeout value expires. Also, when[/color]
          it[color=blue]
          > does execute the Session_end, it seems that the session has already ended
          > and so, I have no access to any of the session variables I was holding[/color]
          onto.[color=blue]
          >
          > Can anybody tell me the server side sequence of events and their handlers
          > that get called when the user closes a browser? Does the browser inform[/color]
          the[color=blue]
          > server that it is closing, so that I can do my cleanup?
          >
          > Any help is highly appreciated.
          >
          > Babu.
          >
          >[/color]


          Comment

          • Kalyan Kumar

            #6
            Re: How to end a session when the user closes the browser?


            write session.RemoveA ll()in the following code


            Sub Application_End (ByVal sender As Object, ByVal e As EventArgs)
            ' Fires when the application ends
            Session.RemoveA ll()
            End Sub

            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            Working...