To begin with, this is the code that I inherited, and the one who wrote it can no longer be here and is not available.
I have users in production who are annoyed when they accidentally exit our ASP.NET MVC application (according to our MVC libraries - version 5). After I looped through the code and website for a while, I think I highlighted what users see in the following code snippet:
public class SessionExpireFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext ctx = HttpContext.Current;
if (ctx.Session != null)
{
if (ctx.Session.IsNewSession)
{
string sessionCookie = ctx.Request.Headers["Cookie"];
if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
{
SessionVariables.Current.User = null;
FormsAuthentication.SignOut();
filterContext.Result = new RedirectResult("~/Account/Logon");
return;
}
}
}
base.OnActionExecuting(filterContext);
}
}
I'm not quite sure why this code is needed (why would you instead of making it an global attribute), but I saw variations of this code on the Internet in my searches, but not with a description of why you want it.
, , , , , , - , ctx.Session , ctx.Session.IsNewSession .
, - Asp.net , , , ( ... ), .
, , , , IsNewSession if. , , 100% ( ). , , , , .
, ctx.User.Identity.IsAuthenticated .
, , , , , Asp.Net , , .
, :
Session.IsNewSession ?- ,
IsNewSession ? - - , , ?
- Asp.Net ?
- , Asp.Net , . ?
Redis . , , Azure ( , ).