What is the difference between Session.Abandon() and Session.Clear()

前端 未结 10 910
陌清茗
陌清茗 2020-11-28 03:36

What is the difference between destroying a session and removing its values? Can you please provide an example demonstrating this?

I searched for this question, but

相关标签:
10条回答
  • 2020-11-28 03:47

    This is sort of covered by the various responses above, but the first time I read this article I missed an important fact, which led to a minor bug in my code...

    Session.Clear() will CLEAR the values of all the keys but will NOT cause the session end event to fire.

    Session.Abandon() will NOT clear the values on the current request. IF another page is requested, the values will be gone for that one. However, abandon WILL throw the event.

    So, in my case (and perhaps in yours?), I needed Clear() followed by Abandon().

    0 讨论(0)
  • 2020-11-28 03:47
    Session.Abandon() 
    

    will destroy/kill the entire session.

    Session.Clear()
    

    removes/clears the session data (i.e. the keys and values from the current session) but the session will be alive.

    Compare to Session.Abandon() method, Session.Clear() doesn't create the new session, it just make all variables in the session to NULL.

    Session ID will remain same in both the cases, as long as the browser is not closed.

    Session.RemoveAll()
    

    It removes all keys and values from the session-state collection.

    Session.Remove()
    

    It deletes an item from the session-state collection.

    Session.RemoveAt()
    

    It deletes an item at a specified index from the session-state collection.

    Session.TimeOut()
    

    This property specifies the time-out period assigned to the Session object for the application. (the time will be specified in minutes).

    If the user does not refresh or request a page within the time-out period, then the session ends.

    0 讨论(0)
  • 2020-11-28 03:51

    this code works and dont throw any exception:

    Session.Abandon();  
    Session["tempKey1"] = "tempValue1";
    

    It's because when the Abandon method is called, the current Session object is queued for deletion but is not actually deleted until all of the script commands on the current page have been processed. This means that you can access variables stored in the Session object on the same page as the call to the Abandon method but not in any subsequent Web pages.

    For example, in the following script, the third line prints the value Mary. This is because the Session object is not destroyed until the server has finished processing the script.

    <% 
      Session.Abandon  
      Session("MyName") = "Mary" 
      Reponse.Write(Session("MyName")) 
    %>
    

    If you access the variable MyName on a subsequent Web page, it is empty. This is because MyName was destroyed with the previous Session object when the page containing the previous example finished processing.

    from MSDN Session.Abandon

    0 讨论(0)
  • this code works and dont throw any exception:
    
    Session.Abandon();  
    Session["tempKey1"] = "tempValue1";
    

    One thing to note here that Session.Clear remove items immediately but Session.Abandon marks the session to be abandoned at the end of the current request. That simply means that suppose you tried to access value in code just after the session.abandon command was executed, it will be still there. So do not get confused if your code is just not working even after issuing session.abandon command and immediately doing some logic with the session.

    0 讨论(0)
  • 2020-11-28 03:57

    When you Abandon() a Session, you (or rather the user) will get a new SessionId (on the next request). When you Clear() a Session, all stored values are removed, but the SessionId stays intact.

    0 讨论(0)
  • 2020-11-28 03:58

    Clearing a session removes the values that were stored there, but you still can add new ones there. After destroying the session you cannot add new values there.

    0 讨论(0)
提交回复
热议问题