【问题标题】:ASP.NET: Session.IsNewSession false after forms timeout, but should be true?ASP.NET:表单超时后 Session.IsNewSession 为假,但应该为真?
【发布时间】:2011-05-23 01:23:53
【问题描述】:

我正在努力设置/更正我的会话超时代码,并查阅了许多文章,例如 this onethis SO post,以了解如何最好地执行此操作。我一遍又一遍地检测会话超时的解决方案是首先检查 Session.IsNewSession 属性是否为真,如果是,则检查会话 cookie 是否已存在。我猜这里的逻辑是用户已经结束了他们最后一个超时的会话,开始了一个新的会话,但是旧的 cookie 还没有被删除。这些检查的代码如下所示:

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"); // or whatever code to handle timeout
        }  
    } 
}

现在我正在使用 120 分钟的会话超时值和 60 分钟的表单超时值。我的 web.config 文件中的这两行分别在这里:

<sessionState mode="InProc" cookieless="UseDeviceProfile" timeout="120" />
<authentication mode="Forms">
    <forms loginUrl="~/Home/Customer" timeout="60" name=".ASPXAUTH" requireSSL="false" slidingExpiration="true" defaultUrl="~/Home/Index" cookieless="UseDeviceProfile" enableCrossAppRedirects="false"/>
</authentication>

因此,在 60 分钟后(我将其设置为 1 进行测试),我向服务器发出请求,并自动重定向到 /Home/Customer,我假设是由于我的 web.xml 中的“loginURL”值。配置行。

问题是会话没有结束,我所有的会话超时检查都在 Home/Customer 操作中(我使用 MVC)。所以我被重定向到 Home/Customer,我通过了上面的检查,但是当我到达 Session.IsNewSession 时,这是错误的,因为会话仍然存在(我假设因为我还在 120 分钟内)设置)。

所以,最后,我的问题。整个会话超时检查方案是否仅在实际 Session 超时时起作用?或者我可以让它也适用于 Forms 超时吗?也许解决方案是将我的会话超时值设置为与表单超时值相同?

【问题讨论】:

    标签: asp.net session-timeout


    【解决方案1】:

    我刚刚测试了用户 John Christensen

    的评价

    如果您的会话超时时间小于表单超时时间。

    web.config

    <system.web>
        <sessionState mode="InProc" timeout="1" />
        <authentication mode="Forms">
          <forms loginUrl="~/Account/Login"  defaultUrl="~/Account/Login" name="MyPortalAuth" timeout="2" />
        </authentication>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
      </system.web>
    

    它似乎是有效的解决方案。

    C# 过滤器属性

    public class SessionExpireFilterAttribute : ActionFilterAttribute
        {
            public UserManager<ApplicationUser> UserManager { get; private set; }
    
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                var ctx = HttpContext.Current;
    
                // check if session is supported
                if (ctx.Session != null)
                {
                    // check if a new session id was generated
                    if (ctx.Session.IsNewSession)
                    {
                        // If it says it is a new session, but an existing cookie exists, then it must
                        // have timed out
                        string sessionCookie = ctx.Request.Headers["Cookie"];
                        if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
                        {
    
                            if (ctx.Request.IsAuthenticated)
                            {
                                FormsAuthentication.SignOut();
                            }
                            RedirectResult rr = new RedirectResult(loginUrl);
                            filterContext.Result = rr;
                            //ctx.Response.Redirect("~/Account/Logon");
    
                        }
                    }                
                }
    
                base.OnActionExecuting(filterContext);
            }
        }
    

    【讨论】:

    • 如何使用 ActionFilter ?
    【解决方案2】:

    ASP.NET 会话 cookie 和 Forms 身份验证 cookie 实际上是完全不同的 cookie - 例如,如果您的应用程序池回收,您的用户将丢失他们的会话但不会丢失他们的登录身份(假设您使用的是进程内会话)。我认为,你的代码会被击中的唯一方法是,如果你的会话超时时间小于你的表单超时时间。任何其他方式,在您点击会话超时代码之前,您将被重定向到登录页面。

    另一种选择是将会话超时代码移动到您的登录页面。

    第三种方法是在 global.asax 中处理检查。

    【讨论】:

    • 目前这些检查在控制器操作中,当用户点击允许他们登录的页面(主页/客户)时运行,我认为这是登录页面,除非你是谈论不同的东西。你能详细说明一下 global.asax 选项吗?
    • 不,你是对的。这只是意味着您不会总是让 IsNewSession 为真;仅当用户空闲时间超过 120 分钟时。
    猜你喜欢
    • 2020-08-23
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2010-11-09
    相关资源
    最近更新 更多