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

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

问题描述

我怎么能摧毁一个会话(会话[名称]),当用户点击注销按钮?

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(姓名); 他们都没有工作。 (我发现这些从谷歌搜索论坛)

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

推荐答案

放弃方法应该工作(的MSDN ):

The Abandon method should work (MSDN):

Session.Abandon();

如果您要删除从会话使用特定项目(<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.remove.aspx\">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天全站免登陆