I'm trying to make a solution for a session. I am using "" variable for the timeout in javascript, so that I only have one single number that dictates the session time. I want to have a javascript file to also check if MVC says the session is ended. Hence, I want to use the session_start and session_end in the process. Since I can't redirect to logout from a VOID session_end, it's recommended to have a seperate javascript session system.
Now, I've instantiated a variable in global.asax to use it in a javascript file. It works only half. Here is what I do:
GLOBAL.ASAX.CS
protected void Session_Start(Object sender, EventArgs e)
{
TraceHelper.WriteException(string.Format(CultureInfo.InvariantCulture,
"User has entered the building:"));
Session["active"] = true;
}
protected void Session_End(Object sender, EventArgs e) {
Session["active"] = false;
TraceHelper.WriteException(string.Format(CultureInfo.InvariantCulture,
"User has left the building:"));
}
_LAYOUT.CSHTML
<script>
var sessionActive = refreshActive();
function refreshActive() {
console.log("refresh = " + "@HttpContext.Current.Session["active"]");
return "@HttpContext.Current.Session["active"]";
}
</script>
SESSION.JS
setTimeout(() => {
sessionActive = refreshActive();
console.log(sessionActive);
}, 31);
Now, don't pay attention to the timing. I've already (succesfully) tested if the session.js function is run AFTER the global.asax session_end is run. I'm sure I see "user has left the building" before seeing any console.logs from the javascript files. Again, it doesn't matter if I refresh the variable a second or half an hour after the global.asax session_end. It always says true. If I change Session["active"] to anything, it's always that same thing (for example, setting it to "asdf" and changing that in the session_end, it still is always "asdf")
I might be missing something really elementary, but I cannot find out what.
P.S. I much rather eleminate the session.js' power since it's a simple timer. I like to call a setter from session_end, that setter in turn calls a javascript logout function. A link/solution to that is also really appreciated.