1.For session expiration, you could do this by managing
Session_End
event in
MvcApplication
class from your
Global.asax.cs
, like in the next example:
protected void Session_End(object sender, EventArgs e)
{
SiteSession siteSession = (Session["SiteSession"] == null ? null : (SiteSession)Session["SiteSession"]);
using (MvcBasicSiteEntities db = new MvcBasicSiteEntities())
{
VisitorLog.OnUserLogoff(db, (siteSession == null ? 0 : siteSession.VisitorLogID), true);
}
}
2.For browser closing you should do it by using JavaScript and AJAX call from your layout (
_Layout.cshtml
) like in the next example:
<script type="text/javascript">
function onWindowClosing() {
if (window.event.clientX < 0 || window.event.clientY < 0) {
$.ajax({
type: "POST",
url: "/Account/OnWindowClosing"
});
}
};
function onKeydown(evt) {
if (evt != undefined && evt.altKey && evt.keyCode == 115)
{
$.ajax({
type: "POST",
url: "/Account/OnWindowClosing"
});
}
};
window.onbeforeunload = onWindowClosing;
window.document.onkeydown = onKeydown;
</script>
And in the controller the action for managing the browser closing will invoke the same method from business logic for updating the visitors log.
public void OnWindowClosing()
{
SiteSession siteSession = this.CurrentSiteSession;
VisitorLog.OnUserLogoff(_db, (siteSession == null ? 0 : siteSession.VisitorLogID), false);
Session.Clear();
}
3.For details and source code about all of these you could use my next article:
MVC Basic Site: Step 4 – jqGrid Integration in MVC 4.0 using AJAX, JSON, jQuery, LINQ, and Serialization[
^]