New session created after RedirectToAction in Asp.net MVC

Wojciech Rajchel 21 Reputation points
2023-05-09T08:30:43.3366667+00:00

In an Asp.net MVC sometimes the session is lost after RedirectToAction to an another controller. I have build in global exception logging in Global.asax in Application_Error but nothing is logged at the moment when session is lost.

The problem happens a few times a day. Can you point me how to proceed to capture the reason of this problem?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,252 questions
{count} votes

1 answer

Sort by: Most helpful
  1. QiYou-MSFT 4,306 Reputation points Microsoft Vendor
    2023-05-12T07:15:02.5766667+00:00

    Hi @Wojciech Rajchel

    First of all, the first point: since there is no cookie, then we write a cookie.

    HttpCookie aCookie = new HttpCookie("cookie1"); 
    aCookie.Value = "";
    aCookie.Expires = DateTime.Now.AddDays(1);
    Response.Cookies.Add(aCookie);
    

    Next, if you want all pages below your site to access this cookie. This is called the scope of the cookie, and you can set it like this:

    HttpCookie aCookie = new HttpCookie("cookie1"); 
    aCookie .Value = "written " + DateTime.Now.ToString(); 
    aCookie .Expires = DateTime.Now.AddDays(1); 
    aCookie .Path = "/";
    Response.Cookies.Add(aCookie );
    

    Best Regards

    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.