How to Kill A Session or Session ID (ASP.NET/C#)

前端 未结 9 1323
礼貌的吻别
礼貌的吻别 2020-11-30 04:09

How can I destroy a session (Session[\"Name\"]) when the user clicks the logout button?

I\'m looking through the ASP.NET API Reference on MSDN and it doesn\'t seem t

9条回答
  •  时光说笑
    2020-11-30 04:22

    From what I tested:

    Session.Abandon(); // Does nothing
    Session.Clear();   // Removes the data contained in the session
    
    Example:
    001: Session["test"] = "test";
    002: Session.Abandon();
    003: Print(Session["test"]); // Outputs: "test"
    

    Session.Abandon does only set a boolean flag in the session-object to true. The calling web-server may react to that or not, but there is NO immediate action caused by ASP. (I checked that myself with the .net-Reflector)

    In fact, you can continue working with the old session, by hitting the browser's back button once, and continue browsing across the website normally.

    So, to conclude this: Use Session.Clear() and save frustration.

    Remark: I've tested this behaviour on the ASP.net development server. The actual IIS may behave differently.

提交回复
热议问题