C#清除会话 [英] C# Clear Session

查看:188
本文介绍了C#清除会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题1

我想知道什么时候我应该使用:

  

<一个href="http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.abandon.aspx">Session.Abandon() //当我用这个追踪过程中并调用它 - 后,我发现会议仍然具有价值。

和我何时应该使用:

  

<一个href="http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.clear.aspx">Session.Clear()

当我应该使用每个特定的方法是什么?


  • 在一般?
  • 在我的具体情况?

我检查,如果会议不是在页面加载等于空。如果会话等于空,我想明确会话和重定向到登录页面?

我应该使用这样的:

 私人无效initSession()
{
    Session.Clear();
    Session.Abandon();
    的Response.Redirect(LoginPage.aspx);
}
 

解决方案

<一个href="http://stackoverflow.com/questions/347377/in-asp-net-when-should-i-use-session-clear-rather-than-session-abandon">In ASP.NET,当我应该使用Session.Clear(),而不是Session.Abandon()?

  

Session.Abandon()销毁会话   和Session_OnEnd事件是   触发。

     

Session.Clear()只是删除所有   值(内容)从对象。该   采用相同的关键还在   活着。

     

因此​​,如果您使用Session.Abandon(),你   失去特定的会话和   用户将获得一个新的会话密钥。您   当用户可以使用它例如   注销。

     

使用Session.Clear(),如果你想要的   留在同一个会话的用户   (如果你不希望他重新登录的   例如)和复位他的所有会话   具体数据。

<一个href="http://stackoverflow.com/questions/1470445/what-is-the-difference-between-session-abandon-and-session-clear">What是Session.Abandon()和Session.Clear()

之间的区别
  

清除 - 移除所有的键和值   从会话状态集合。

     

放弃 - 删除所有对象   存储在一个会话。如果你不   显式调用Abandon方法,   服务器将删除这些目的和   破坏了会话时的会话   超时。这也提出了类似的活动   Session_End中。

     

Session.Clear可以比作   从货架移除所有书籍,   而Session.Abandon更像   扔掉了整个货架。

     

...

     

通常,在大多数情况下,需要   使用Session.Clear。您可以使用   Session.Abandon如果您确信   用户要离开你的网站。

     

于是回差异:

     
      
  • 在放弃加薪Session_End中的请求。
  •   
  • 清除立即删除项目,放弃不。
  •   
  • 放弃释放将sessionState对象及其项目,以便它能垃圾   集。
  •   
  • 清除保持SessionState会以及与之相关联的资源。
  •   

<一个href="http://forums.asp.net/t/1689196.aspx/1?Session%20Clear%20or%20Session%20Abandon%20">Session.Clear()或Session.Abandon()?

  

您使用Session.Clear()的时候你不在   要结束会话,而是   只是清除所有的会话密钥   并重新初始化会话。

     

Session.Clear()不会引起   Session_End中事件处理程序在你的   Global.asax文件来执行。

     

但是,另一方面   Session.Abandon()将删除   会议共并且将执行   Session_End中事件处理程序。

     

Session.Clear()就像是去除书籍   从书架上

     

Session.Abandon()就像抛   我的书架本身。

问题

我查了一些会议,如果在页面加载不等于空。如果其中一人等于空我想清除所有的会话和重定向到登录页面?

答案

如果您希望用户重新登录,使用Session.Abandon。

Question #1

I want to know when am I supposed to use:

Session.Abandon() // When I use this during tracing and after calling it- I find the session still has a value.

And when am I supposed to use :

Session.Clear()

When should I use each specific method?


  • In general?
  • In my specific case?

I check if session is not equal null in Page Load. If session is equal to null, I wanna to clear session and redirect to the login page?

Should I use something like this:

private void initSession()
{
    Session.Clear();
    Session.Abandon();
    Response.Redirect("LoginPage.aspx");
}

解决方案

In ASP.NET, when should I use Session.Clear() rather than Session.Abandon()?

Session.Abandon() destroys the session and the Session_OnEnd event is triggered.

Session.Clear() just removes all values (content) from the Object. The session with the same key is still alive.

So, if you use Session.Abandon(), you lose that specific session and the user will get a new session key. You could use it for example when the user logs out.

Use Session.Clear(), if you want that the user remaining in the same session (if you don't want him to relogin for example) and reset all his session specific data.

What is the difference between Session.Abandon() and Session.Clear()

Clear - Removes all keys and values from the session-state collection.

Abandon - removes all the objects stored in a Session. If you do not call the Abandon method explicitly, the server removes these objects and destroys the session when the session times out. It also raises events like Session_End.

Session.Clear can be compared to removing all books from the shelf, while Session.Abandon is more like throwing away the whole shelf.

...

Generally, in most cases you need to use Session.Clear. You can use Session.Abandon if you are sure the user is going to leave your site.

So back to the differences:

  • Abandon raises Session_End request.
  • Clear removes items immediately, Abandon does not.
  • Abandon releases the SessionState object and its items so it can garbage collected.
  • Clear keeps SessionState and resources associated with it.

Session.Clear() or Session.Abandon() ?

You use Session.Clear() when you don't want to end the session but rather just clear all the keys in the session and reinitialize the session.

Session.Clear() will not cause the Session_End eventhandler in your Global.asax file to execute.

But on the other hand Session.Abandon() will remove the session altogether and will execute Session_End eventhandler.

Session.Clear() is like removing books from the bookshelf

Session.Abandon() is like throwing the bookshelf itself.

Question

I check on some sessions if not equal null in the page load. if one of them equal null i wanna to clear all the sessions and redirect to the login page?

Answer

If you want the user to login again, use Session.Abandon.

这篇关于C#清除会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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