You need JS to poll the server periodically (e.g. every minute using setTimeout
) to ask am I still logged in?
. If it gets false
as a response, your JS can do whatever you'd like (show a message, redirect to the login page etc).
The server side implementation will be something like
[HttpGet]
[AllowAnonymous]
[DoNotExtendAuthentication]
public ActionResult IsAuthenticated(string url)
{
var isAuthenticated = HttpContext?.User?.Identity?.IsAuthenticated ?? false;
return Json(isAuthenticated); // change this line to whatever shape of JSON you want
}
public class DoNotExtendAuthenticationAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.HttpContext.Response.Cookies.Remove(
FormsAuthentication.FormsCookieName);
}
}
DoNotExtendAuthenticationAttribute
is needed to effectively disable forms auth's sliding expiration logic.
For your routing, be sure to use the SessionlessHandler so that it doesn't continually extend the Session
.