简体   繁体   中英

ASP.NET MVC: LogOut Heartbeat function

Basically I am looking for a reliable log out mechanism when the user closes his tab. This includes calling some server code and thus all client side only mechanisms like deleting the cookie wont work for me.

On the internet you mostly find the approach to intercept the window.unload function and then place some code in there. I know there is the possibility to filter out normal navigation requests from other events that might trigger window.unload , but I generally don't like this approach, as I have to make some sort of synchronous AJAX call in order to reliable execute some custom logout code on the server, which is definitely not the best way of achieving what I want.

There is the second approach of implementing a Heartbeat function to periodically check if the client responds. I do think this is the best approach for me / my scenario, but I am actually struggling with the implementation with ASP.NET MVC.

How would I approach this in ASP.NET MVC? I already thought of SignalR, but it's actually not directly possible to access session information within the SignalR context.

It is easy to find some fancy implementations of this (see, for instance, this question ).

For a basic heartbeat implementation you will need three files: the HTML (the website page sending the beats), the controller (receiving the beats) and the JavaScript file (with your heartbeat function). You may include the JavaScript code in the HTML file, but it's better practice to keep it separated.

Here you have the exact contents of my working example:

HTML file (Heart.cshtml)

Note that you need to include both the JavaScript file and jQuery. @Url.Action("ReceiveHeartbeat", "Auxiliary") gives the address to the method (ReceiveHeartbeat) in the controller (AuxiliaryController).

<head>
    <script type="text/javascript" src="~/Scripts/heartBeat.js"></script>
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>

<body>        
    <script type="text/javascript">
        LaunchHeartBeat('@Url.Action("ReceiveHeartbeat", "Auxiliary")');
    </script>
</body>

Javascript (heartBeat.js)

Other people use setTimeout , but if you want a loop using setTimeout is cleaner. I would recommend writing two functions, using setInterval(myFunction, myInterval); (see below). You can add more stuff to the Ajax request (for instance, a success: action).

function LaunchHeartBeat(actionUrl) {
    var intervalMilliseconds = 750;
    setInterval(function () {
        $.ajax({
            type: "POST",
            url: actionUrl
        });
    }, intervalMilliseconds);
}

Controller (AuxiliaryController.cs)

[HttpPost]
public JsonResult KeepSessionAlive()
{
    // You may do stuff here
    return new JsonResult { Data = "success" };
}

Edit : The alternative syntax for the Javascript file:

var actionUrl = null;
var intervalMilliseconds = 1000;

function LaunchHeartBeat(myUrl) {
    actionUrl = myUrl; 
    setInterval(HeartBeat, intervalMilliseconds); 
}

function HeartBeat() {
    $.ajax({
        type: "POST",
        url: actionUrl
    });
}

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