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

前端 未结 9 1271
礼貌的吻别
礼貌的吻别 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:37

    Session.Abandon()

    This marks the session as Abandoned, but the session won't actually be Abandoned at that moment, the request has to complete first.

    0 讨论(0)
  • 2020-11-30 04:46
    Session["YourItem"] = "";
    

    Works great in .net razor web pages.

    0 讨论(0)
  • 2020-11-30 04:48

    The Abandon method should work (MSDN):

    Session.Abandon();
    

    If you want to remove a specific item from the session use (MSDN):

    Session.Remove("YourItem");
    

    EDIT: If you just want to clear a value you can do:

    Session["YourItem"] = null;
    

    If you want to clear all keys do:

    Session.Clear();
    

    If none of these are working for you then something fishy is going on. I would check to see where you are assigning the value and verify that it is not getting reassigned after you clear the value.

    Simple check do:

    Session["YourKey"] = "Test";  // creates the key
    Session.Remove("YourKey");    // removes the key
    bool gone = (Session["YourKey"] == null);   // tests that the remove worked
    
    0 讨论(0)
提交回复
热议问题