How to Update database record if session TimeOut or Close Window for LoggedIn Users?

In MVC Application I have Users table named "tbl_agenti", when the users log-in open Session and update Table column named "IsOnline" to true, than if users click LogOut link, also work and update again column in table to false. My problem is how to solve if Session TimeOut, or close window to update Database?

Here is code of Controller for

LOGIN

[HttpPost]
        public ActionResult Login(tbl_agenti user)
        {
            using (BTEntity db = new BTEntity())
            {
                var usr = db.tbl_agenti.Where(u => u.username == user.username && u.Password == user.Password).FirstOrDefault();
                if (usr != null)
                {
                    Session["aId"] = usr.aID.ToString();
                    Session["username"] = usr.username.ToString();
                    Session["agent_ime"] = usr.agent_ime.ToString();
                    Session["rullID"] = usr.rullID.ToString();

                    tbl_agenti online = new tbl_agenti();
                    online = db.tbl_agenti.Find(usr.aID);
                    online.IsOnline = true;
                    db.Entry(online).State = EntityState.Modified;
                    db.SaveChanges();
                   return RedirectToAction("../rezervacii/IndexAKT");

                }
                else
                {

                    ModelState.AddModelError("", "Username или Password е погрешен!");
                }
                return View();
            }
        }

LOGGED IN

 public ActionResult LoggedIn()
        {

            if (Session["aID"] != null )
                {
                return View();
            }
            else
            {
                return RedirectToAction("", "Login");
            }
        }

LOGOUT

public ActionResult LogOut()
        {
       tbl_agenti online = new tbl_agenti();
        online = db.tbl_agenti.Find(Convert.ToInt32(Session["aID"]));
        online.IsOnline = false;
        db.Entry(online).State = EntityState.Modified;
        db.SaveChanges();
        Session.Clear();
        return RedirectToAction("", "UserManager/Login");
    }

UPDATED

I found this Javascript, and work ok for close window, but i have problem if i refresh page from browser, with F5 not problem, just Refresh/Reload button on browser when i click then Session clear and loge me Out. How to prewent Refresh button?

var spcKey = false;
var hover = true;
var contextMenu = false;

function spc(e) {
    return ((e.altKey || e.ctrlKey || e.keyCode == 91 || e.keyCode == 87) && e.keyCode != 82 && e.keyCode != 116);
}

$(document).hover(function () {
    hover = true;
    contextMenu = false;
    spcKey = false;
}, function () {
    hover = false;
}).keydown(function (e) {
    if (spc(e) == false) {
        hover = true;
        spcKey = false;
    }
    else {
        spcKey = true;
    }
}).keyup(function (e) {
    if (spc(e)) {
        spcKey = false;
    }
}).contextmenu(function (e) {
    contextMenu = true;
}).click(function () {
    hover = true;
    contextMenu = false;
});

window.addEventListener('focus', function () {
    spcKey = false;
});
window.addEventListener('blur', function () {
    hover = false;
});

window.onbeforeunload = function (e) {
    if ((hover == false || spcKey == true) && contextMenu == false) {
        window.setTimeout(goToLoginPage, 100);
        $.ajax({
            url: "/UserManager/LogOut",
            type: 'post',
            data: {},
        });
    }
    return;
};

function goToLoginPage() {
    hover = true;
    spcKey = false;
    contextMenu = false;
    location.href = "/UserManager/LogIn";
}