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

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

问题描述

问题 #1

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

I want to know when am I supposed to use:

Session.Abandon()//当我在跟踪期间和调用它之后使用它 - 我发现会话仍然有一个值.

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

我应该什么时候使用:

Session.Clear()

我应该什么时候使用每种特定的方法?

When should I use each specific method?

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

我在页面加载中检查 session 是否不等于 null.如果 session 等于 null,我想清除 session 并重定向到登录页面?

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");
}

推荐答案

在 ASP.NET 中,我什么时候应该使用 Session.Clear() 而不是 Session.Abandon()?

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

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

Session.Clear() 只是删除所有来自对象的值(内容).这使用相同密钥的会话仍然存在活着.

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

所以,如果你使用 Session.Abandon(),你失去那个特定的会话和用户将获得一个新的会话密钥.你可以使用它,例如当用户退出.

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.

如果需要,请使用 Session.Clear()保持在同一会话中的用户(如果你不想让他重新登录示例)并重置他的所有会话具体数据.

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.

Session.Abandon() 和 Session.Clear()

清除 - 删除所有键和值来自会话状态集合.

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

放弃 - 删除所有对象存储在会话中.如果你不显式调用 Abandon 方法,服务器删除这些对象并会话时销毁会话超时.它还引发诸如会话_结束.

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 可以比作从书架上取下所有书籍,而 Session.Abandon 更像是扔掉整个架子.

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

...

一般来说,在大多数情况下你需要使用 Session.Clear.您可以使用Session.Abandon 如果你确定用户将要离开您的网站.

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.

回到差异:

  • 放弃会引发 Session_End 请求.
  • Clear 会立即删除项目,Abandon 不会.
  • Abandon 释放 SessionState 对象及其项目,以便它可以垃圾集.
  • Clear 保持 SessionState 和与之关联的资源.

Session.Clear() 或Session.Abandon() ?

当你不使用 Session.Clear() 时想要结束会话,而是只需清除会话中的所有键并重新初始化会话.

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() 不会导致Session_End 事件处理程序在您的要执行的 Global.asax 文件.

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

但另一方面Session.Abandon() 将删除会话完全并将执行Session_End 事件处理程序.

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

Session.Clear() 就像移书从书架上

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

Session.Abandon() 就像扔书架本身.

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

问题

如果在页面加载中不等于 null,我会检查一些会话.如果其中一个等于 null,我想清除所有会话并重定向到登录页面?

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?

回答

如果您希望用户再次登录,请使用 Session.Abandon.

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

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

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