【问题标题】:Why Doesn't Asp.Net Session.Clear() Work On Close Tab When I See It Run?当我看到它运行时,为什么 Asp.Net Session.Clear() 在关闭选项卡上不起作用?
【发布时间】:2013-04-29 15:17:20
【问题描述】:

当主管和其他人使用的工作场所系统的选项卡关闭时,我一直试图从会话中彻底休息。当此人注销时,我就成功了-一切都被清除了。但是,当一个选项卡关闭时,它可以随意重新打开,就像从未调用过 session.clear() 一样。

我正在使用这个 javascript:

 $.ajax({
    type: "POST",
    url: "default.aspx/EndSession",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) { }
 });

调用这个网络方法:

[WebMethod]
public static string EndSession()
{
   HttpContext.Current.Session.Clear();
   HttpContext.Current.Session.Abandon();
   HttpContext.Current.User = null;
   FormsAuthentication.SignOut();
   return "";
}

我设置了一个断点并观察代码在我单步执行时执行并返回。选项卡关闭,然后我右键单击并选择“重新打开关闭的选项卡”,页面返回并且会话仍然处于活动状态。

我看到here ppl 说这是不可能的,但不明白为什么,因为代码正在服务器上运行 - 我正在看着它运行,并且会话没有被清除。我原以为这是对“Session.Clear()”和“Session.Abandon()”的显式调用。注销 Page_Load 中的相同代码效果很好。

为什么?我错过了什么? session clear 运行后是否因为tab关闭而被抛出?

谢谢!

【问题讨论】:

  • this answer。 IMO,唯一真正的解决方案是在服务器端存储一些东西,而不仅仅依赖于会话 cookie。
  • 现在朝这边走。会话可能会在多个选项卡中打开,因此会增加复杂性,但现在我将从您指出的建议开始。感谢您提供的信息 - 我会在成功后回复(希望如此)。

标签: asp.net security session webforms


【解决方案1】:

尝试将Session.RemoveAll();redirect 也添加到另一个页面?

public static string EndSession()
{
   HttpContext.Current.Session.Clear();
   HttpContext.Current.Session.Abandon();
   HttpContext.Current.Session.RemoveAll();
   HttpContext.Response.Redirect("~/login.aspx", true);
   HttpContext.Current.User = null;
   FormsAuthentication.SignOut();
   return "";
}


除了上面的代码添加Page_Load:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoServerCaching();
HttpContext.Current.Response.Cache.SetNoStore();

我不是 100% 确定它会起作用,我想写它希望它可以有所帮助。

【讨论】:

  • 添加了您建议的行。我之前有缓存的东西,刚刚尝试了重定向 - 我已经从 javascript 尝试过它,但它被忽略了(可能是设计使然)。代码隐藏也没有运气。所以 OP 中的 cmets 可能是我最好的选择。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
相关资源
最近更新 更多