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

前端 未结 10 911
陌清茗
陌清茗 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:59

    I think it would be handy to use Session.Clear() rather than using Session.Abandon().

    Because the values still exist in session after calling later but are removed after calling the former.

    0 讨论(0)
  • 2020-11-28 04:01

    Existence of sessionid can cause the session fixation attack that is one of the point in PCI compliance. To remove the sessionid and overcome the session fixation attack, read this solution - How to avoid the Session fixation vulnerability in ASP.NET?.

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

    Clear - Removes all keys and values from the session-state collection.

    Abandon - removes all the objects stored in a Session. If you do not call the Abandon method explicitly, the server removes these objects and destroys the session when the session times out.
    It also raises events like Session_End.

    Session.Clear can be compared to removing all books from the shelf, while Session.Abandon is more like throwing away the whole shelf.

    You say:

    When I test Session, it doesn't makes any change when I Abandon the session.

    This is correct while you are doing it within one request only.
    On the next request the session will be different. But the session ID can be reused so that the id will remain the same.

    If you will use Session.Clear you will have the same session in many requests.

    Generally, in most cases you need to use Session.Clear.
    You can use Session.Abandon if you are sure the user is going to leave your site.

    So back to the differences:

    1. Abandon raises Session_End request.
    2. Clear removes items immidiately, Abandon does not.
    3. Abandon releases the SessionState object and its items so it can ba garbage collected to free the resources. Clear keeps SessionState and resources associated with it.
    0 讨论(0)
  • 2020-11-28 04:07

    clear-its remove key or values from session state collection..

    abandon-its remove or deleted session objects from session..

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