简体   繁体   English

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

[英]How to remove specific session in asp.net?

I am facing a problem.我正面临一个问题。 I have created two sessions:我创建了两个会话:

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

I know how to remove sessions using Session.clear() .我知道如何使用Session.clear()删除会话。 I want to remove the session "userType".我想删除会话“userType”。

How do I remove a specific session?如何删除特定会话?

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

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;

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

Session.Remove("yourSessionName");

There are many ways to nullify session in ASP.NET.在 ASP.NET 中有很多方法可以使会话无效。 Session in essence is a cookie, set on client's browser and in ASP.NET, its name is usually ASP.NET_SessionId . Session 本质上是一个 cookie,设置在客户端的浏览器上,在 ASP.NET 中,它的名字通常是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.因此,理论上如果您删除该 cookie(就浏览器而言,这意味着您将其到期日期设置为过去的某个日期,因为开发人员无法删除 cookie),那么您将丢失服务器中的会话。 Another way as you said is to use Session.Clear() method.你所说的另一种方法是使用Session.Clear()方法。 But the best way is to set another irrelevant object (usually null value) in the session in correspondance to a key.但最好的方法是在会话中设置另一个不相关的对象(通常为null值)对应一个键。 For example, to nullify Session["FirstName"] , simply set it to Session["FirstName"] = null .例如,要使Session["FirstName"]无效,只需将其设置为Session["FirstName"] = null

HttpContext.Current.Session.Remove("sessionname"); HttpContext.Current.Session.Remove("sessionname");

it works for me这个对我有用

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

Session["your_session"] = null;

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

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