【问题标题】:Detect ASP.NET Session Timeout - ASP.NET_SessionId method not working检测 ASP.NET 会话超时 - ASP.NET_SessionId 方法不起作用
【发布时间】:2013-04-27 16:32:30
【问题描述】:

我正在尝试检测 Asp.NET 会话超时以将用户重定向到超时页面;我检查了各种方法,其中大多数类似于

http://aspalliance.com/520_Detecting_ASPNET_Session_Timeouts.2

(如果Session.IsNewSessionASP.NET_SessionId cookie存在,则为超时)

问题是“ASP.NET_SessionId”cookie 始终存在于我面前,即使我刚刚开始调试,因此在第一次启动网站时总是给我一个错误的超时标志。

更新: 为了测试,我刚刚使用以下代码创建了一个空的 Asp.NET Web 应用程序:

BasePage.cs

using System;
using System.Web.UI;

namespace TestApp.classes
{
    public class BasePage : Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (Context.Session != null)
            {
                if (Session.IsNewSession)
                {
                    string szCookieHeader = Request.Headers["Cookie"];
                    if ((null != szCookieHeader) && (szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
                    {
                        Response.Redirect("sessionTimeout.htm");
                    }
                }
            }
        }
    }
}

Global.asax

using System;

namespace TestApp
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {
            var a = "";
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            var b = "";
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}

WebForm1.aspx

using System;
using TestApp.classes;

namespace TestApp
{
    public partial class WebForm1 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

Web.config

<?xml version="1.0"?>
<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
      <sessionState timeout="1"></sessionState>
    </system.web>

</configuration>

然后按 F5,我将被重定向到 sessionTimeout.htm。为什么?

【问题讨论】:

  • 如果cookie始终存在,为什么要检查它?只需要检查Session.IsNewSession 或删除cookie
  • 因为理论上 ASP.NET_SessionId cookie 在第一次访问时不会出现。
  • 您必须使用 Cookie.Discard 属性,您可以在 web.config 中更改 cookie 状态为 false。
  • 谢谢,能详细解释一下吗?

标签: asp.net session session-cookies


【解决方案1】:

您在开发网站时不必使用 cookie。还有其他存储数据的方法,例如您可以将特定于用户的数据存储在数据库中

注意web.configSessionState元素的"Cookieless = false"属性,这意味着当前会话的sessionID将作为cookie保存在客户端机器中

<sessionState cookieless="true" />

ASP.NET 会话状态的默认设置在machine.config 文件中定义,并且可以在应用程序根文件夹中的web.config 文件中被覆盖。通过确保上述行出现在根 web.config 文件中,您可以启用无 cookie 会话

参考http://msdn.microsoft.com/en-us/library/aa479314.aspx

所以只检查Session.IsNewSession 并禁用cookie

【讨论】:

  • 在所有方法中,我都没有看到有人建议使用 cookieless 模式(我需要 cookie);也许我的配置有其他问题?
猜你喜欢
  • 2014-12-13
  • 1970-01-01
  • 1970-01-01
  • 2010-09-30
  • 2016-07-11
  • 2018-12-31
  • 2014-12-05
  • 2010-09-16
  • 2018-12-25
相关资源
最近更新 更多