I use cookie authentication in MVC5 . My web pages rely heavily on authenticated as well as unauthenticated Ajax calls every 1-5 seconds to update the data. Therefore, my users never log out.
My ideal scenario: if the user is actively viewing or conducting activities on my site, continue the session. If they left the page open after 10 minutes, I would like their session to be a timeout and I will use Ajax crashes to redirect to the login page. I think this is best done at the controller or action level.
I tried to control the behavior of the session state, as suggested below, but the session still did not leave. After 65 seconds of hitting ReadOnly / Public once per second, I call ReadOnly / Authorized and successfully retrieve data from it.
Here is my CookieAuthentication configuration.
public void ConfigureAuth(IAppBuilder app) {
My test page:
<div id="public"></div> <div id="authorized"></div> @section scripts{ <script> function poll(times) { var url = '/ReadOnly/Public'; $.ajax({ url: url, dataType: 'json', data: null, cache: false, success: function (data) { $('#public').html(times + ' ' + data.test); }, error: function (data) { $('#public').html(times + ' ' + 'failed'); } }); }; function checkAuth(times) { var url = '/ReadOnly/Authorized'; $.ajax({ url: url, dataType: 'json', data: null, cache: false, success: function (data) { $('#authorized').html(times + ' ' + data.test); }, error: function (data) { $('#authorized').html(times + ' ' + 'failed'); } }); }; $(function () { var times = 1; setInterval(function () { poll(times); times++; }, 1000); setInterval(function () { checkAuth(times); }, 65000); }); </script> }
and the code of the test controller (I tried this with both disabled and readonly settings)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.SessionState; namespace SessionTest.Controllers { [SessionState(SessionStateBehavior.ReadOnly)] public class ReadOnlyController : Controller { [Authorize] public ActionResult Authorized() { return Json(new { test = "ReadOnly and Authorized" }, JsonRequestBehavior.AllowGet); } public ActionResult Public() { return Json(new { test = "ReadOnly and Public" }, JsonRequestBehavior.AllowGet); } } }
ajax asp.net-mvc-5
Bill shihara
source share