【问题标题】:how to maintain session state as long as possible with asp.net 4.0如何使用 asp.net 4.0 尽可能长时间地保持会话状态
【发布时间】:2014-05-05 21:41:43
【问题描述】:

我只想让会话尽可能长。在这里,我将介绍我的代码如何使用 ashx 文件与 c# 的 asp.net 4.0 尽可能长时间地保持会话。

这是我的 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>

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

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

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

【问题讨论】:

  • timeout = "2" 表示您的会话将每 2 分钟到期一次。您要查看的是禁用 IIS 中的会话超时。不在代码中。 IIS 是维持会话活动的平台。 .Net 只能设置 IIS 可以读取和修改其行为的属性。所以在谷歌中查找“iis session timeout never”以获得一些答案。
  • 这个 2 分钟的超时是测试目的,我想知道这个处理程序是否有效并能活着。阅读我之前的question
  • 就像我说的:你想查看 IIS 设置,而不是 in-code 或 web.config... IIS 可以让你的会话永远持续下去。

标签: c# asp.net session


【解决方案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>

【讨论】:

    猜你喜欢
    • 2012-11-26
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 2019-09-15
    • 2019-06-15
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    相关资源
    最近更新 更多