locked
how to clear session variables after closing the browser or rewriting the url RRS feed

  • Question

  • User1343581070 posted

    Hi,

            I have defined  a session variable session("MyCart") = Nothing in the global.asax's session_start event. It gets a value when products are added to the cart. Now if i close the browser and restart the application the session("MyCart") variable is still available . How do I set it to nothing when the browser closes or when i rewite the website url in the address bar ?

    Please help

    regards

    Sanjish 

    Monday, October 24, 2016 6:47 AM

All replies

  • User-183374066 posted

    Session is related to users. It isn't shared across all the users. Global variables/ static variables/ Application object shared across users.

    Try open the url in different browser, it will not have any value in other browser

    Read more about sessions and this also

    See following for Global variables

    http://stackoverflow.com/questions/4171089/how-to-define-a-global-variable-in-asp-net-web-app

    https://www.dotnetperls.com/global-variables-aspnet

    Also go through following for more clarifications

    http://stackoverflow.com/questions/2565812/asp-net-session-or-global-variables

    http://stackoverflow.com/questions/6768038/global-variables-vs-asp-net-session-state

    Monday, October 24, 2016 7:00 AM
  • User1343581070 posted

    Hi ,

              Thanks Nasser for all the helpful links. But I want to be able to kill the session variable (set it to Nothing) when browser closes or on a fresh visit to the website.

    Regards

    Sanjish

    Monday, October 24, 2016 11:02 AM
  • User347430248 posted

    Hi Sanjish,

    you can try to use the code mentioned below to clear the session.

     <script type="text/javascript"> 
          
                    $(document).ready(function () { 
     
                        window.addEventListener('beforeunload',recordeCloseTime); 
     
                  }); 
        
                     function recordeCloseTime() { 
     
                        $.ajax({ type: "POST", 
     
                        url: "ServiceToClearSession.asmx/RecordCloseTime", 
                     });      
                    } 
                </script> 
             
    The following Service is triggered from the Ajax call on default.aspx to clear the session on browser close, ALT + F4 
     
                [System.Web.Script.Services.ScriptService] 
                public class ServiceToClearSession : System.Web.Services.WebService 
                { 
                    [WebMethod(EnableSession = true)] 
                    public void RecordCloseTime() 
                    { 
                        HttpContext.Current.Session.Clear(); 
                        SessionInfoDataSource dataSource = new SessionInfoDataSource(); 
                        SessionInfo newSessionInfo = new SessionInfo() 
                            { 
                                SessionValue = "Current Session value is " + Session["SessionCreatedTime"], 
                                BrowserClosedTime = DateTime.Now 
                            }; 
                        dataSource.InsertSessionInfo(newSessionInfo); 
     
                        dataSource.Save(); 
                    } 
                } 
    
    

    Reference:

    How to clear session when the web browser is closed in ASP.NET

    you can also try to check the marked Answer of the thread mentioned below. in that op had a same issue like yours.

    End Session When Browser Closed

    Regards

    Deepak

    Wednesday, October 26, 2016 1:17 AM
  • User753101303 posted

    Hi,

    Some are trying to do that when the page unloads (but you have then to carefully distinguish between a real unload and just going to another page and so you could quite easily introduced other issues). The intended usage is to either provide the user an explicit logout button on which you can clear explicitely the session (Session.Abandon) or to just let the session expires.

    Tuesday, December 13, 2016 10:30 AM
  • User-1479952219 posted

    <script type="text/javascript">

    $(document).ready(function(){

    window.addEventListener('beforeunload',recordeCloseTime);});

    function recordeCloseTime(){

    $.ajax({type:"POST",

    url:"ServiceToClearSession.asmx/RecordCloseTime",

    });

    }

    </script>

    [System.Web.Script.Services.ScriptService]

    public class ServiceToClearSession : System.Web.Services.WebService

    {

    [WebMethod(EnableSession = true)]

    public void RecordCloseTime()

    {

    HttpContext.Current.Session.Clear();

    SessionInfoDataSource dataSource = new SessionInfoDataSource();

    SessionInfo newSessionInfo = new SessionInfo()

    {

    SessionValue = "Current Session value is " + Session["SessionCreatedTime"],

    BrowserClosedTime=DateTime.Now

    };

    dataSource.InsertSessionInfo(newSessionInfo);

    dataSource.Save();

    }

    }

    Wednesday, December 14, 2016 6:26 AM