如何使用asp.net 4.0尽可能长时间地维护会话状态

时间:2014-05-05 13:12:48

标签: c# asp.net session

我只想尽可能长时间地开会。在这里,我提供了我的代码如何使用asp.net 4.0使用aspx 4.0使用aspx文件保持会话。

这是我的keepalive.ashx文件:

<%@ WebHandler Language="C#" Class="keepalive" %>

using System;
using System.Web;
using System.Web.SessionState;

public class keepalive : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        if (context.User.Identity.IsAuthenticated)
        {
            // authenticated sessions
            context.Response.ContentType = "text/plain";
            context.Response.Write("Auth:" + context.Session.SessionID);
        }
        else
        {
            // guest
            context.Response.ContentType = "text/plain";
            context.Response.Write("NoAuth:" + context.Session.SessionID);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

这是我在母版页中的调用代码:

<script type="text/javascript">
        var interval = null;
        (function () {
            // keep me alive
            interval = setInterval(function () {
                $.get('../keepalive.ashx', function (d) {
                    $('#response').append(d + '<br/>');
                });
            }, 30000);
        })();


        // If we want to stop the interval....
        function Stop() {
            clearInterval(interval);
        }
</script>

我从web配置中更改了一些属性:

 <sessionState mode="InProc" cookieless="false" timeout="2"/>

这段代码怎么不满足我的需要.. 请帮帮我......

1 个答案:

答案 0 :(得分:1)

您可以通过web.config(已经执行了2分钟)为应用程序配置超时值,如下所示

<configuration>
    <system.web>
      ...
      <sessionState timeout="525600 "/>
    </system.web>

</configuration>

或者您也可以通过设置IIS site上提供的IIS配置在所有应用程序的服务器上设置它

<location path="Default Web Site">
   <system.webServer>
      <asp>
         <session allowSessionState="true" max="1000" timeout="525600" />
      </asp>
   </system.webServer>
</location>