asp.net mvc 会话在托管环境中经常过期
如果其他人遇到同样的问题并得到解决,将不胜感激分享他的解决方案:
问题: 我在 asp.net mvc3 中完成了网站,会话经常过期,在几秒到最多 5 分钟之间。在 web.config 中,我将超时设置为 2880 分钟,还将会话状态设置为 stateserver。我还删除了超时以使用默认的超时,仍然存在问题:
<sessionState mode="StateServer"></sessionState>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn"/>
</authentication>
我制作了一个示例网站,仅包含登录功能和一些数据库交互来模拟环境,然后我得到了 2 分 50 秒的更稳定的超时(测试了 9 次) ),这仍然不正确,因为我在 web.config 中设置了 2880 分钟。
当用户登录时,在代码中,我创建一个 AuthenticateTicket 并将其保存到 cookie,然后在安全页面中检查 User.Identity,并且控制器具有过滤器 [Authorize],因此当会话过期时,我会被发送到登录页面。
在同一台托管服务器上,我还有其他使用 .NET 3.5 Web 表单的旧应用程序,对于它们来说,会话没问题,仅对于新的 mvc3 和 mvc3,会话经常过期。感谢您提供的任何线索。
In case someone else has same problem and got it solved, would appreciate share his solution:
Problem:
I have website done in asp.net mvc3, the session expires often, between some seconds to max 5 minutes. In web.config I have set the timeout to 2880 min, I also set the sessionstate to stateserver. I also delete the timeouts to use the default ones, still problem:
<sessionState mode="StateServer"></sessionState>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn"/>
</authentication>
I made a sample website that only contains the login functions and some database interaction to simulate the environment, then i got a more stable timeout of 2 min 50 secs (tested 9 times), which still is not correct since I set 2880 min in the web.config.
Inside the code when user login, I create an AuthenticateTicket and save it to cookies, then in secured pages I checked for User.Identity and the controllers have the filter [Authorize], so when session expires I'm sent to login page.
On the same hosting server I have other older apps using .NET 3.5 web forms, and for them the session is ok, only for new mvc3 and mvc3 the session is expiring often. Thanks for any clue you can give.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后:
我的具体问题是我的主机 (Arvixe) 为每个网站设置了 100 MB 内存的限制。由于映射到数据库的复杂查询,使用 EF(以及可能的任何 ORM)会增加内存使用量,因此我的网站使用了超过 100 MB 的内存,导致应用程序池由于 100MB 内存限制而重新启动,因此会话被重置。
解决方案:
对于我的具体情况,将每个网站设置为使用共享应用程序池暂时解决了问题,同时我按照第一个选项上的帖子优化我的网站,这样我就可以达到 Arvixe 的 100MB 内存限制。
Final:
My specific problem was my hosting (Arvixe) has set a limit of 100 MB memory for each website. Using EF (and likely any ORM) increases memory usage due to complex queries mapped to the database, so my websites were using more than 100 MB, causing the application pool to restart due to the 100MB memory limitation, hence Session was reset.
Solutions:
For my specific case, setting each website to use shared app pool solved the problem temporary, while I follow the post on 1st option to optimize my websites so I can go under Arvixe's limit of 100MB memory.
我遇到了类似的问题,我的会话在 30 秒左右停止,这只发生在我的开发环境中。
我在这里找到了我的解决方案:
http://forum.winhost.com/showthread.php?t=9017
尝试将机器密钥添加到您的网站。
I had a similar issue my session was stopping at around 30 seconds, this only happened on my development environment.
I found my solution here:
http://forum.winhost.com/showthread.php?t=9017
Try to add your a machine key to your website.
这可能是使用共享托管时的常见问题,原因如下:
您与其他客户共享服务器,并且 ASP.NET 工作进程经常重新启动。
您的主机正在使用负载平衡服务器,并且您正在访问一台服务器,然后在后续访问中您将访问另一台没有会话数据的服务器。
最佳解决方法是:
1,如果您的主机有 SQL 数据库,请查看为共享会话状态设置数据库。
2,编写代码时不要长时间依赖会话中的值 - 始终首先进行检查,如果对象不在会话中,则重建对象。
3、考虑迁移到 VPS 或升级您的计划。
This can be a common problem when using shared hosting, for the following reasons:
You are sharing a server with other customers and the ASP.NET worker process is being restarted frequently.
Your host is using load balanced servers and you are visiting one server and then on a subsequent visit you are then hitting another server where there is no session data.
Best workarounds are:
1, If you have a SQL database with your host have a look at setting up the database for shared session state.
2, When writing code don't depend on values in the session for a long duration - always do a check first and if the object is not in session then rebuild the object.
3, Consider moving to a VPS or upgrading your plan.