C# 如何删除asp.net中的特定会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8971625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:23:51  来源:igfitidea点击:

How to remove specific session in asp.net?

c#asp.netsession

提问by Chintan

I am facing a problem. I have created two sessions:

我正面临一个问题。我创建了两个会话:

  1. Session["userid"] = UserTbl.userid;
  2. Session["userType"] = UserTbl.type;
  1. Session["userid"] = UserTbl.userid;
  2. Session["userType"] = UserTbl.type;

I know how to remove sessions using Session.clear(). I want to remove the session "userType".

我知道如何使用Session.clear(). 我想删除会话“userType”。

How do I remove a specific session?

如何删除特定会话?

采纳答案by Milan Mendpara

There is nothing like session container , so you can set it as null

没有像会话容器那样的东西,因此您可以将其设置为 null

but rather you can set individual session element as null or ""

而是您可以将单个会话元素设置为 null 或 ""

like Session["userid"] = null;

喜欢 Session["userid"] = null;

回答by gabsferreira

Session.Remove("name of your session here");

回答by Ravi Gadag

you can use Session.Remove() method; Session.Remove

您可以使用 Session.Remove() 方法;会话.删除

Session.Remove("yourSessionName");

回答by Saeed Neamati

There are many ways to nullify session in ASP.NET. Session in essence is a cookie, set on client's browser and in ASP.NET, its name is usually ASP.NET_SessionId. So, theoretically if you delete that cookie (which in terms of browser means that you set its expiration date to some date in past, because cookies can't be deleted by developers), then you loose the session in server. Another way as you said is to use Session.Clear()method. But the best way is to set another irrelevant object (usually nullvalue) in the session in correspondance to a key. For example, to nullify Session["FirstName"], simply set it to Session["FirstName"] = null.

在 ASP.NET 中有很多方法可以使会话无效。Session本质上是一个cookie,设置在客户端的浏览器上,在ASP.NET中,它的名字通常是ASP.NET_SessionId. 因此,理论上如果您删除该 cookie(就浏览器而言,这意味着您将其到期日期设置为过去的某个日期,因为开发人员无法删除 cookie),那么您将丢失服务器中的会话。你说的另一种方法是使用Session.Clear()方法。但是最好的方法是null在会话中设置另一个不相关的对象(通常是值)与一个键相对应。例如,要使 无效Session["FirstName"],只需将其设置为Session["FirstName"] = null

回答by user3645907

A single way to remove sessions is setting it to null;

删除会话的一种方法是将其设置为 null;

Session["your_session"] = null;

回答by ssh

if (HttpContext.Current.Session["sessionname"] != null) HttpContext.Current.Session.Remove("sessionname");

if (HttpContext.Current.Session["sessionname"] != null) HttpContext.Current.Session.Remove("sessionname");

it works for me

这个对我有用