如何终止会话或会话 ID (ASP.NET/C#) [英] How to Kill A Session or Session ID (ASP.NET/C#)

查看:35
本文介绍了如何终止会话或会话 ID (ASP.NET/C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在用户单击注销按钮时销毁会话 (Session["Name"])?

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

我正在查看 MSDN 上的 ASP.NET API 参考,但似乎没有太多信息.好像比较有限.但是我找不到 ASP.NET 类等的任何其他页面.

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.

我试过了:

Session.Abandon();Session.Contents.Remove("Name"); 它们都不起作用.(我在 Google 搜索的论坛中找到了这些)

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

推荐答案

Abandon 方法应该有效 (MSDN):

The Abandon method should work (MSDN):

Session.Abandon();

如果您想从会话中删除特定项目,请使用 (MSDN):

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

Session.Remove("YourItem");

如果您只想清除一个值,您可以这样做:

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.

简单检查:

Session["YourKey"] = "Test";  // creates the key
Session.Remove("YourKey");    // removes the key
bool gone = (Session["YourKey"] == null);   // tests that the remove worked

这篇关于如何终止会话或会话 ID (ASP.NET/C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆