ASP .NET MVC 3.会话过期太快了

Sir*_*lly 3 asp.net web-config session-cookies asp.net-mvc-3

我有一个页面(View),它在某些时间间隔内发送AJAX查询.用户可以很长时间使用此页面.但会议在大约40-60分钟后到期.所以AJAX请求不会返回有用的信息.

我的Web.config

<system.web>
  <sessionState
      timeout="259200"
      cookieName="SunTest.SessionId"
      regenerateExpiredSessionId="true"
      sqlCommandTimeout="200"
      stateNetworkTimeout="200">
  </sessionState>
<roleManager enabled="true" defaultProvider="SqlProvider" cacheRolesInCookie="true" cookieName=".ASPROLES" cookieTimeout="259200" cookiePath="/" cookieRequireSSL="false" cookieSlidingExpiration="true" cookieProtection="All">
  <providers>
    <add name="SqlProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="SqlServices" applicationName="/" />
  </providers>
</roleManager>
<authentication mode="Forms">
  <forms loginUrl="~" timeout="259200" protection="All" />
</authentication>   
Run Code Online (Sandbox Code Playgroud)

我已经改变了我的web.config

<appSettings>
    <add key="enableSimpleMembership" value="false" />
    <add key="autoFormsAuthentication" value="false" />
</appSettings>
<system.web>
  <sessionState 
      mode="SQLServer"
      allowCustomSqlDatabase="true"
      sqlConnectionString="Data Source=servername;Initial Catalog=dbname;User ID=username;Password=password"
      timeout="259200"
      cookieName="SunTest.SessionId"
      regenerateExpiredSessionId="true"
      sqlCommandTimeout="200"
      stateNetworkTimeout="200">
  </sessionState>
<roleManager createPersistentCookie="true" enabled="true" defaultProvider="SqlProvider" cacheRolesInCookie="true" cookieName=".ASPROLES" cookieTimeout="259200" cookiePath="/" cookieRequireSSL="false" cookieSlidingExpiration="true" cookieProtection="All">
  <providers>
    <add name="SqlProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="SqlServices" applicationName="/" />
  </providers>
</roleManager>
<authentication mode="Forms">
  <forms domain="help2b-2.hosting.parking.ru" name="ASPXFORMSAUTH" path="/" loginUrl="~" slidingExpiration="true" cookieless="UseCookies" timeout="259200" requireSSL="false" />
</authentication>
Run Code Online (Sandbox Code Playgroud)

它使这些饼干: Google Chrome屏幕截图

所以,有3个cookie:

  • SunTest.SessionId.301字节.到期 - 会话.

  • ASPXFORMSAUTH.301字节.到期 - 会话.

  • .ASPROLES.565字节.到期 - 星期二,2012年7月10日04:14:48 GMT

但是几分钟后(大约30-40),它会删除.ASPROLES cookie.用户已退出.因此,AJAX查询不起作用.

这个配置有什么问题?

小智 5

您的所有用户是否同时失去会话状态?如果是这样,您的应用程序池可以回收.有几个原因导致这种情况发生.

我建议您考虑设置用户会话管理,以便能够在应用程序和会话重新启动后继续运行. 这里有一些选择.