如何在asp.net mvc中处理会话

时间:2022-05-24 03:29:12

How can I do this in Asp.Net MVC?

我怎么能在Asp.Net MVC中这样做?

  • Activity Timer : When user navigates on the page (means doing something that calls ActionResult) the timer will reset.
  • 活动计时器:当用户在页面上导航时(意味着执行调用ActionResult的操作),计时器将重置。
  • When the user doesn't do any activity in more than 15 minutes, he will be prompted and be asked "Are you still there?" (Yes/LogOut), if he click yes, then activity timer will go back to zero, and starting counting again.
  • 当用户在超过15分钟内没有进行任何活动时,系统会提示他并询问“你还在吗?” (是/ LogOut),如果他单击是,则活动计时器将返回到零,并再次开始计数。
  • This activity timer must be unique per user and must be destroy upon logging out.
  • 此活动计时器必须是每个用户唯一的,并且必须在注销时销毁。

PLEASE PLEASE! Be PATIENT TO ME! I don't know how to this.

请你好!耐心等待我!我不知道怎么回事。

I had posted this question but I didn't get any answer that solves this problem. So I plan to do it from the start, and I need some help/guide to do it properly.
Any reference links or tutorials will be much appreciated!

我发布了这个问题,但我没有得到解决这个问题的任何答案。所以我打算从一开始就这样做,我需要一些帮助/指导来正确地做到这一点。任何参考链接或教程将不胜感激!

1 个解决方案

#1


1  

I would handle this on the client. Create an object to encapsulate the functionality.

我会在客户端上处理这个问题。创建一个对象来封装功能。

Something like:

就像是:

var UserInactivityMonitor = UserInactivityMonitor || {};
UserInactivityMonitor = (function (module) {
    var inactivityIndex;
    var promptIndex;
    var timer;

    module.startInactivityMonitor = function () {
        module.timerTick();
    };

    module.timerTick = function () {
        inactivityIndex--;
        if (inactivityIndex === 0) {
            module.fireInactivityAlert();
        }

        if (inactivityIndex === promptIndex) {
            module.fireIdlePrompt();
        }

        timer = setTimeout(module.timerTick, 1000);
    };

    module.fireIdlePrompt = function () {
        var response = confirm('are you stil there?');
        if (response === true) {
            module.resetInactivityIndex();
        }
    };

    module.resetInactivityIndex = function () {
        inactivityIndex = 15;
        promptIndex = 5;
    };

    module.fireInactivityAlert = function () {
        alert('Inactivity alert!');
    };

    module.initialize = function () {
        module.resetInactivityIndex();
        module.startInactivityMonitor();
    };

    return module;
})(UserInactivityMonitor || {});

Set inactivityIndex to the number of seconds that will pass before the inactivity event fires. Set promptIndex to the number of seconds remaining when the user will be prompted if they are still there. The code above sets the inactivity timeout to 15 seconds and a idle prompt will be invoked at the 5 second remaining mark.

将inactivityIndex设置为在不活动事件触发之前将经过的秒数。将promptIndex设置为提示用户是否仍然存在时剩余的秒数。上面的代码将不活动超时设置为15秒,并在剩余的5秒标记处调用空闲提示。

On page load start the inactivity timer:

在页面加载时启动不活动计时器:

$(function () {
    UserInactivityMonitor.initialize();
});

On any AJAX request, reset the counter:

在任何AJAX请求中,重置计数器:

 $("#fooButton").on('click', function () {
        $.ajax(
            {
                url: $("#buttonClickPostUrl").val(),
                data: {
                    someData: 'data'
                },
                type: 'POST',
                complete: function () {
                    UserInactivityMonitor.resetInactivityIndex();
                }
            });
    });

If the server is maintaining session state then you will want to make a request back to the server to kill the session and optionally direct the browser to the appropriate page for the event. You would do this in the fireInactivityAlert() function.

如果服务器正在维护会话状态,那么您将需要向服务器发出请求以终止会话,并可选择将浏览器定向到该事件的相应页面。您可以在fireInactivityAlert()函数中执行此操作。

#1


1  

I would handle this on the client. Create an object to encapsulate the functionality.

我会在客户端上处理这个问题。创建一个对象来封装功能。

Something like:

就像是:

var UserInactivityMonitor = UserInactivityMonitor || {};
UserInactivityMonitor = (function (module) {
    var inactivityIndex;
    var promptIndex;
    var timer;

    module.startInactivityMonitor = function () {
        module.timerTick();
    };

    module.timerTick = function () {
        inactivityIndex--;
        if (inactivityIndex === 0) {
            module.fireInactivityAlert();
        }

        if (inactivityIndex === promptIndex) {
            module.fireIdlePrompt();
        }

        timer = setTimeout(module.timerTick, 1000);
    };

    module.fireIdlePrompt = function () {
        var response = confirm('are you stil there?');
        if (response === true) {
            module.resetInactivityIndex();
        }
    };

    module.resetInactivityIndex = function () {
        inactivityIndex = 15;
        promptIndex = 5;
    };

    module.fireInactivityAlert = function () {
        alert('Inactivity alert!');
    };

    module.initialize = function () {
        module.resetInactivityIndex();
        module.startInactivityMonitor();
    };

    return module;
})(UserInactivityMonitor || {});

Set inactivityIndex to the number of seconds that will pass before the inactivity event fires. Set promptIndex to the number of seconds remaining when the user will be prompted if they are still there. The code above sets the inactivity timeout to 15 seconds and a idle prompt will be invoked at the 5 second remaining mark.

将inactivityIndex设置为在不活动事件触发之前将经过的秒数。将promptIndex设置为提示用户是否仍然存在时剩余的秒数。上面的代码将不活动超时设置为15秒,并在剩余的5秒标记处调用空闲提示。

On page load start the inactivity timer:

在页面加载时启动不活动计时器:

$(function () {
    UserInactivityMonitor.initialize();
});

On any AJAX request, reset the counter:

在任何AJAX请求中,重置计数器:

 $("#fooButton").on('click', function () {
        $.ajax(
            {
                url: $("#buttonClickPostUrl").val(),
                data: {
                    someData: 'data'
                },
                type: 'POST',
                complete: function () {
                    UserInactivityMonitor.resetInactivityIndex();
                }
            });
    });

If the server is maintaining session state then you will want to make a request back to the server to kill the session and optionally direct the browser to the appropriate page for the event. You would do this in the fireInactivityAlert() function.

如果服务器正在维护会话状态,那么您将需要向服务器发出请求以终止会话,并可选择将浏览器定向到该事件的相应页面。您可以在fireInactivityAlert()函数中执行此操作。