【问题标题】:unexplained Session time out in ASP.NETASP.NET 中无法解释的会话超时
【发布时间】:2015-05-28 16:47:23
【问题描述】:

我有 Webform ASP.NET 4.5 应用程序。

在登录页面Session变量设置为:(相关代码简化)

 Session["UserName"] = txtUserName.Text; (txtUserName.Text cannot be empty)

然后

Response.Redirect("Survey.aspx");

在调查页面我有

if (Session == null || Session["UserName"] == null)
    {
        string errorText = "Session was timed out due to inactivity, to  continue, please close All of your Browser windows and log in again";

在 web.config 文件中我有:

<system.web>    
    <sessionState mode="InProc" timeout="1200" />   
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <customErrors mode="Off"> </customErrors>
</system.web>

我也有 IIS 本身

超时:02:00:00

仍有用户在 15-20 分钟后间歇性地报告会话超时。

这是什么原因造成的?

==============================

更新:将其设置为状态服务器后,我得到:

Unable to make the session state request to the session state server. Please ensure that the ASP.NET State service is started and that the client and server ports are the same.  If the server is on a remote machine, please ensure that it accepts remote requests by checking the value of HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters\AllowRemoteConnection.  If the server is on the local machine, and if the before mentioned registry value does not exist or is set to 0, then the state server connection string must use either 'localhost' or '127.0.0.1' as the server name. 

【问题讨论】:

  • 看你的应用程序池回收行为的配置。它可能被配置为定期回收,这将导致 InProc 会话丢失。
  • 我还看到池从其他进程中回收,例如防病毒扫描文件。
  • 如果您将会话设置为 StateServer 而不是 InProc,则会话信息应在池回收后继续存在。
  • @Joe,非常好的观点。设置为空闲超时分钟 (20) 这可能是造成这种情况的原因吗?

标签: c# asp.net vb.net iis


【解决方案1】:

如 cmets 中所述,您的应用程序池可能配置为定期回收,这将导致 InProc 会话丢失。

你也在 cmets 中问过:

您有什么建议更改应用程序池或

一般来说,我都不会这样做!相反,我会设计应用程序,使其能够抵御 Session 数据丢失。

在您的示例中,您将用户名存储在 Session 中。相反,我会使用表单身份验证,在这种情况下,您可以从HttpContext.Current.User.Identity.Name 获得用户名:无需将其存储在 Session 中。

一般来说,我只会在 Session 中存储可以轻松重新生成的东西,例如通过从数据库中读取。要从 Session 中检索内容,请检查 null 并在必要时重新生成,例如:

var mySessionValue = (MyType) Session["MyKey"];
if (mySessionValue == null)
{
    mySessionValue = ... regenerate value, e.g. by reading from database
    Session["MyKey"] = mySessionValue;
}
...

【讨论】:

  • 谢谢。在 IIS 中,我看到网站超时设置为 20 分钟。但是在那个网站下,我的 Survey Web 应用程序会话超时 ID 设置为 120。第二个会覆盖第一个吗?
  • @SNash - 由于您使用的是 InProc 会话,是的,它确实“覆盖”了第一个会话。应用程序池会在 20 分钟后回收,这会导致内存中的任何内容丢失,包括 Session、ASP.NET Cache、静态字段、...
  • 非常有趣。如果我启用 Formauthentication ,我该如何填充 'HttpContext.Current.User.Identity.Name'?我记得我之前尝试过一次,''HttpContext.Current.User.Identity.Name'' 正在返回 Windows 用户。不是在我的登录表单中登录的人。
  • @SNash - 如果您启用了 Windows 身份验证,它将返回一个 Windows 用户。我建议您阅读表单身份验证,如果您有任何问题,请发布另一个问题,这是一个开始的地方:msdn.microsoft.com/en-us/library/7t6b43z4(v=vs.140).aspx
【解决方案2】:

您可能会丢失会话状态,因为正在回收应用程序池。应用程序池可能被回收的原因有很多,包括超时、异常等。

如果您将会话状态从 InProc 更改为 StateServer,那么您的会话信息应该在应用程序池被回收后仍然存在。

使用StateServer 有一些缺点——主要的缺点是添加到会话中的对象必须是可序列化的。这通常不是什么大问题。

要使StateServer 工作,您需要确保已安装ASP.NET State Server 并且服务正在运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 2010-10-13
    • 1970-01-01
    • 2011-12-08
    相关资源
    最近更新 更多