Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to kill the session when user closes browser in asp.net 2.0

How to kill the session when user closes browser in asp.net 2.0

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netsecurityhelptutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Raheem MA
    wrote on last edited by
    #1

    Hi friends, We are developing a web application that uses sessions and Forms authentication. I want to call Session.Abandon() to kill the session when the user closes the browser window(from any page of the application after logging in). How to achieve this? Please shed some light on this to help me. Thank you, Shah_tech

    S N 2 Replies Last reply
    0
    • R Raheem MA

      Hi friends, We are developing a web application that uses sessions and Forms authentication. I want to call Session.Abandon() to kill the session when the user closes the browser window(from any page of the application after logging in). How to achieve this? Please shed some light on this to help me. Thank you, Shah_tech

      S Offline
      S Offline
      sashidhar
      wrote on last edited by
      #2

      I think You are looking for this..1 http://aspalliance.com/1294_CodeSnip_Handle_Browser_Close_Event_on_the_ServerSide.all[^]

      LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

      1 Reply Last reply
      0
      • R Raheem MA

        Hi friends, We are developing a web application that uses sessions and Forms authentication. I want to call Session.Abandon() to kill the session when the user closes the browser window(from any page of the application after logging in). How to achieve this? Please shed some light on this to help me. Thank you, Shah_tech

        N Offline
        N Offline
        nainakarri
        wrote on last edited by
        #3

        Hi You can call a javascript function on closing the browser. In that javascript function navigate to a logout page where you can then kill the session.

        function AlertForBrowserClose()
        {

                    var clicked=false;
                    var winClosed=false;
                  
                    if (window.screenTop&&(window.screenTop>screen.height))
                    {
                      winClosed=true;
                    }
                    if ((!clicked) && (!winClosed)) 
                    {
                         //This part wiil be excuted OnClick of REFRESH
                    }
                    else if(window.event.clientY < 0 && window.event.clientX < 0)
                    {     
                           //This part wiil be excuted OnClick of BROWSER Close(X)  
                           
                    var MCPageName =  document.location.href.substring
                    (   
                    document.location.href.length , document.location.href.lastIndexOf('/') + 1 
                    );             
                     
                     if(MCPageName!="MCAlerts.aspx")
                     {
                         window.location.href = '../logout.aspx';          
                         alert('Please use proper "LogOut" for next sessions!');  
                     }                                                   
                             
                           
                       
                    }
           
            }
        

        I have used alert statement alert('Please use proper "LogOut" for next sessions!'); Make sure that this alert is used. Else the logout page will not get hit and session soes not end.

        Naina

        R 1 Reply Last reply
        0
        • N nainakarri

          Hi You can call a javascript function on closing the browser. In that javascript function navigate to a logout page where you can then kill the session.

          function AlertForBrowserClose()
          {

                      var clicked=false;
                      var winClosed=false;
                    
                      if (window.screenTop&&(window.screenTop>screen.height))
                      {
                        winClosed=true;
                      }
                      if ((!clicked) && (!winClosed)) 
                      {
                           //This part wiil be excuted OnClick of REFRESH
                      }
                      else if(window.event.clientY < 0 && window.event.clientX < 0)
                      {     
                             //This part wiil be excuted OnClick of BROWSER Close(X)  
                             
                      var MCPageName =  document.location.href.substring
                      (   
                      document.location.href.length , document.location.href.lastIndexOf('/') + 1 
                      );             
                       
                       if(MCPageName!="MCAlerts.aspx")
                       {
                           window.location.href = '../logout.aspx';          
                           alert('Please use proper "LogOut" for next sessions!');  
                       }                                                   
                               
                             
                         
                      }
             
              }
          

          I have used alert statement alert('Please use proper "LogOut" for next sessions!'); Make sure that this alert is used. Else the logout page will not get hit and session soes not end.

          Naina

          R Offline
          R Offline
          Raheem MA
          wrote on last edited by
          #4

          Thank you for quick reply. One thing is should I write the code in all the aspx pages of my application? How can we kill the session when the browser is closed without writing the code in every page? Thank you, Shah_tech

          N 1 Reply Last reply
          0
          • R Raheem MA

            Thank you for quick reply. One thing is should I write the code in all the aspx pages of my application? How can we kill the session when the browser is closed without writing the code in every page? Thank you, Shah_tech

            N Offline
            N Offline
            nainakarri
            wrote on last edited by
            #5

            Are you using master page in your application which is referred in all the aspx pages. If so you can write the code in master page and redirect to logout page. If not i guess you should write in every aspx page. Regards Naina

            Naina

            D 1 Reply Last reply
            0
            • N nainakarri

              Are you using master page in your application which is referred in all the aspx pages. If so you can write the code in master page and redirect to logout page. If not i guess you should write in every aspx page. Regards Naina

              Naina

              D Offline
              D Offline
              Dinesh Mani
              wrote on last edited by
              #6

              or just put it in a .js file and include it in all the pages.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups