Clear Session in ASP.Net

Posted by Jignesh on Stack Overflow See other posts from Stack Overflow or by Jignesh
Published on 2010-06-09T07:57:22Z Indexed on 2010/06/09 8:12 UTC
Read the original article Hit count: 348

Filed under:
|

I want to clear the session on Page unload.

Here is a condition :

  • If user goes from Page A to Page B of the same site session must not get cleared.
  • If user close the browser window or Tab(close the site),session must gets cleared.

I have tried using AJAX PageMethod to call server-side procedure to remove session from client side script.But the procedure is not getting hit,I have checked it using Breakpoint.

server side procedure is in master.cs file

I will appreciate your help.

Here is code in site.master

<body onunload="HandleClose();">
    <script type="text/javascript">
        function HandleClose()
        {            
            PageMethods.AbandonSessions();
        }

and here is a code in master.cs :

[System.Web.Services.WebMethod]
        public static void AbandonSessions()
        {

            HttpContext.Current.Session.Abandon();

        }

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about session