登录会话有时会丢失时asp.net MVC 3重定向到行动 [英] Login Session lost sometimes when redirect to action in asp.net mvc 3

查看:164
本文介绍了登录会话有时会丢失时asp.net MVC 3重定向到行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然部署一个asp.net MVC 3项目中,我得到了这个问题最近我不知道为什么它发生了。

While deploy an asp.net mvc 3 project, I got the issue recently which I have no idea why it happened.

我有一个类名为LoginSesion将获得经过验证的用户,当用户登录,并存储在会话中。

I have a class named LoginSesion which will get the Authenticated User when user login and store in session.

public static LoginSession AuthenticatedUser
    {
        get
        {
            if (HttpContext.Current.Session["LoginSession"] != null)
                return HttpContext.Current.Session["LoginSession"] as LoginSession;
            return null;
        }
        set
        {
            HttpContext.Current.Session["LoginSession"] = value;
        }
    }

当我运行该项目,尽量用户重定向到特定的URL(如的http://本地主机/用户/细节/ 1 )的HttpContext.Current。会话[LoginSession]来为null,并且重定向用户返回到登录页面。

When I run the project, try to redirect user to a specific URL (e.g. http://localhost/user/details/1), the HttpContext.Current.Session["LoginSession"] come to Null and redirect user back to Login page.

奇怪的是,这并不总是空,只是有时候。当服务器运行速度太慢,它发生得虽然会话不会过期呢。

The weird thing is this does not always null, just sometimes. And when server runs too slow, it happens too although the session has not expire yet.

我在web.config中设置会话超时如下:

I have set in the web.config the session timeout as follow:

<authentication mode="Forms">
  <forms loginUrl="~/UserProfiles/Logon" timeout="2880" />
</authentication>

<sessionState mode="InProc" timeout="2880" />

我使用的是IIS 7.x的发布和测试。

I'm using IIS 7.x for publishing and testing.

推荐答案

您必须考虑以下几点:

1)的sessionState 暂停更新(从0重新启动)每次用户调用的页面。相比之下,格式 暂停每一个用户调用页面和<$ C中的至少一半时更新$ C>暂停(2880你的情况)传递。这意味着,这两个超时不同步。

1) The sessionState timeout is updated (restart from 0) every time a user calls a page. By contrast, the form timeout is updated every time a user calls a page AND at least half of the timeout (2880 in your case) is passed. That means, the two timeouts are not synchronized.

2)注意在IIS应用程序池空闲超时属性。它是关于在此之后,应用程序池被回收的时间。这意味着,会话变量都将丢失。

2) Pay attention to the Idle Time-out property in IIS application pool. It is about the time after which the application pool is recycled. That means that session variables are lost.

这篇关于登录会话有时会丢失时asp.net MVC 3重定向到行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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