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

2020-01-27 03:06发布

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 to have much information. It seems rather limited. But I cannot find any other pages for ASP.NET Classes etc.

I have tried:

Session.Abandon(); and Session.Contents.Remove("Name"); neither of them work. ( I found these in a forum from a Google search)

9条回答
smile是对你的礼貌
2楼-- · 2020-01-27 03:59

Session.Abandon() this will destroy the data.

Note, this won't necessarily truly remove the session token from a user, and that same session token at a later point might get picked up and created as a new session with the same id because it's deemed to be fair game to be used.

查看更多
何必那么认真
3楼-- · 2020-01-27 04:03

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.

查看更多
\"骚年 ilove
4楼-- · 2020-01-27 04:11

You kill a session like this:

Session.Abandon()

If, however, you just want to empty the session, use:

Session.Clear()
查看更多
登录 后发表回答