简体   繁体   English

如何终止会话或会话 ID (ASP.NET/C#)

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

How can I destroy a session (Session["Name"]) when the user clicks the logout button?当用户单击注销按钮时,如何销毁会话 (Session["Name"])?

I'm looking through the ASP.NET API Reference on MSDN and it doesn't seem to have much information.我正在查看 MSDN 上的 ASP.NET API 参考,它似乎没有太多信息。 It seems rather limited.好像比较有限。 But I cannot find any other pages for ASP.NET Classes etc.但是我找不到 ASP.NET 类等的任何其他页面。

I have tried:我试过了:

Session.Abandon(); and Session.Contents.Remove("Name");Session.Contents.Remove("Name"); neither of them work.他们都没有工作。 ( I found these in a forum from a Google search) (我在 Google 搜索的论坛中找到了这些)

The Abandon method should work ( MSDN ): Abandon方法应该有效( MSDN ):

Session.Abandon();

If you want to remove a specific item from the session use ( MSDN ):如果要从会话中删除特定项目,请使用 ( 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

It is also a good idea to instruct the client browser to clear session id cookie value.指示客户端浏览器清除会话 ID cookie 值也是一个好主意。

Session.Clear();
Session.Abandon();
Response.Cookies["ASP.NET_SessionId"].Value = string.Empty;
Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddMonths(-10);

Session.Abandon() 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.这将会话标记为 Abandoned,但此时会话实际上不会被 Abandoned,请求必须首先完成。

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. Session.Abandon 只将会话对象中的布尔标志设置为 true。 The calling web-server may react to that or not, but there is NO immediate action caused by ASP.调用Web服务器可以反应到或没有,但没有造成ASP立即采取行动。 (I checked that myself with the .net-Reflector) (我用 .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.所以,总结一下:使用 Session.Clear() 并避免挫折。

Remark: I've tested this behaviour on the ASP.net development server.备注:我已经在 ASP.net 开发服务器上测试了这种行为。 The actual IIS may behave differently.实际 IIS 的行为可能有所不同。

Session["YourItem"] = "";

在 .net razor 网页中效果很好。

Session.Abandon(); did not work for me either.也不适合我。

The way I had to write it to get it to work was like this.我必须编写它才能让它工作的方式是这样的。 Might work for you too.也可能对你有用。

HttpContext.Current.Session.Abandon();

Session.Abandon() this will destroy the data. Session.Abandon()这将破坏数据。

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.请注意,这不一定会真正从用户那里删除会话令牌,并且稍后可能会使用相同的会话令牌作为具有相同 ID 的新会话被拾取并创建,因为它被认为是可以使用的公平游戏。

You kill a session like this:你像这样杀死一个会话:

Session.Abandon()

If, however, you just want to empty the session, use:但是,如果您只想清空会话,请使用:

Session.Clear()
Session.Abandon()

is what you should use.是你应该使用的。 the thing is behind the scenes asp.net will destroy the session but immediately give the user a brand new session on the next page request.事情在幕后 asp.net 会破坏会话,但会在下一个页面请求时立即为用户提供一个全新的会话。 So if you're checking to see if the session is gone right after calling abandon it will look like it didn't work.因此,如果您正在检查会话是否在调用放弃后立即消失,它将看起来不起作用。

-列出项目xasadads

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM