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

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

问题描述:

Session.Clear() 和 Session.Abandon() 都摆脱了会话变量.据我了解,Abandon() 结束当前会话,并导致创建一个新会话,从而触发 End 和 Start 事件.

Both Session.Clear() and Session.Abandon() get rid of session variables. As I understand it, Abandon() ends the current session, and causes a new session to be created thus causing the End and Start events to fire.

在大多数情况下调用 Abandon() 似乎更可取,例如注销用户.是否有我会使用 Clear() 代替的场景?性能差异很大吗?

It seems preferable to call Abandon() in most cases, such as logging a user out. Are there scenarios where I'd use Clear() instead? Is there much of a performance difference?

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

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 the user to relogin for example) and reset all the session specific data.