Home > other >  Session variables lost ASP.Net
Session variables lost ASP.Net

Time:01-03

I have Visual Studio Professional 2019, licensed, in a Windows Server 2019 virtual machine.

I have moved a project from my computer to the server, but when debugging, the session variables are lost between methods; in my pc they were working fine with Visual Studio Community 2019. I have tried disabling antivirus but still doesnt work.

This is the code where I save the value in the Session:

if (nombre != null)
                {
                    Session["Usuario"] = nombre;

                }

                ViewBag.error = "";
                return RedirectToAction("Inicio");

            }
            else
            {

                ViewBag.error = "Usuario o contraseña incorrectos";
                return View("login");

But when checking the Session in the view, there is no ["Usuario"] array.

I execute it in debug mode, IIS Express Chrome. Dont know if it could interfere, but I have installed in the same machine the IIS.

CodePudding user response:

IIS6 joins the function of application pool to recycle some useless processes. When the application pool is caused by errors in website programs or too many visits, the process will be automatically recycled to prevent the website from entering the "crash" state. At this time, The recycling of the application pool will cause the session variable to be cleared, and the session variable will disappear. In order to solve this problem, we start the ASP.NET State Service service on the server side, and make some changes in the system's machine.config. The session state mode is now StateServer by default. If you also have a web.config configuration file in the root directory of your website, please change mode="InProc" to mode="StateServer", and the following code can prevent the loss of session variables:

<sessionState mode="InProc" cookieless="false" timeout="60"/>

Change to

<sessionState
mode="StateServer"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="20"
/>

CodePudding user response:

I have finally made it work. I tried commenting this line in my web.config

<sessionState mode="InProc" cookieless="false" timeout="2400"/>

And the variables started working again

  • Related