如何在asp.net 3.5中尽可能长时间地活动会话

时间:2014-04-04 08:20:14

标签: c# javascript asp.net session

我正在使用asp.net 3.5。我只想尽可能长时间地活动会话(浏览器关闭)。在这里,我只是按照以下步骤操作:

1)更改为Web配置的会话超时值失败。 2)使用一些外部.ashx文件,并在主页面上使用javascript调用,如下所示:

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

using System;
using System.Web;
using System.Web.SessionState;
public class SessionHeartbeat : IHttpHandler, IRequiresSessionState
{
   public void ProcessRequest(HttpContext context)
    {
        context.Session["Heartbeat"] = DateTime.Now;
    }

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

主页上的这个javascript:

     <script type="text/javascript">
    function setHeartbeat() {
    setTimeout("heartbeat()", 300000); // every 5 min
}

function heartbeat() {
     $.post(
        "../SessionHeartbeat.ashx?Timestamp" + new Date().toString()",
        null,
        function(data) {
            //$("#heartbeat").show().fadeOut(1000); // just a little "red flash" in the corner :)
            setHeartbeat();
        },
        "json"
    );
}
</script>

但它并没有给我我想要的结果。 请帮帮我......

------------------------------------ Upadated ------- --------------------------------

这里我修改了我的jquery:

 <script type="text/javascript">
    function setHeartbeat() {
    setTimeout("heartbeat()", 300000); // every 5 min
}

function heartbeat() {
     $.get(
        "../SessionHeartbeat.ashx",
        null,
        function(data) {
           beatHeart(2); // just a little "red flash" in the corner :)
            setHeartbeat();
        },
        "json"
    );
}
// beat the heart 
// 'times' (int): nr of times to beat
function beatHeart(times) {
    var interval = setInterval(function () {
        $(".heartbeat").fadeIn(500, function () {
            $(".heartbeat").fadeOut(500);
        });
    }, 1000); // beat every second

    // after n times, let's clear the interval (adding 100ms of safe gap)
    setTimeout(function () { clearInterval(interval); }, (1000 * times) + 100);
}
</script>

---------------------------------- Upadted --------- -------------------------------

enter image description here

1 个答案:

答案 0 :(得分:1)

这是OP的一个工作示例。

Web.config - 设置测试的小超时

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

Javscript (在此示例的页面中)

<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>

这会在名为响应的元素中打印sessionId,以便我们可以看到会话ID。

keepalive.ashx 通用处理程序(需要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);
        }
    }

注意:有一件事没有被问到是你的keepalive应该在经过身份验证的会话上运行吗?如果是这样,我想知道是否还存在cookie过期问题?