What does it mean if my Asp.Net session has IsNewSession == true and I don't care?

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;

        // check if session is supported
        if (ctx.Session != null)
        {

            // check if a new session id was generated
            if (ctx.Session.IsNewSession)
            {

                // If it says it is a new session, but an existing cookie exists, then it must
                // have timed out
                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 ( , ).

+4
1

ASP.NET , . , - , . , , . " ", ctx.Session . , , , , .

, , , - . . . , , , , , . (, , , , "" .)

, - , , , , , ASP.NET , . , , , , , , , .

+2

Source: https://habr.com/ru/post/1606653/


All Articles