简体   繁体   中英

How to handle session in asp.net mvc

How can I do this in Asp.Net MVC?

  • Activity Timer : When user navigates on the page (means doing something that calls ActionResult ) the timer will reset.
  • 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.
  • 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!

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.

On page load start the inactivity timer:

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

On any AJAX request, reset the counter:

 $("#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM