title: Session variables lost mvc
body: <p>I have an MVC 3 application, it use  asp validation
  when the user logs in the app the validadion succeed and sotore user name in a session variable.</p>
  
  <p>At here everithing works fine. </p>
  
  <p>then in the AccountController i Set a RedirectionToAction, to another controller, here the session variables are lost. </p>
  
  <pre><code>**//HERE THE VARIABLES ARE LOST AND AN ERROR HAPPENS**
  return RedirectToAction("menuOtbr", "Menu", new { area = "Configuracion" });
  </code></pre>
  
  <p>I've tryed </p>
  
  <ol>
  <li>Change from InProc to ServerState and problem persists, </li>
  <li>Deactivate my AV.</li>
  <li><p>Adding protected <strong>void Session_Start(){}</strong> nothing happens. the session doesn't start or restart again.</p></li>
  <li><p>Other many suggestions almost all articles published with this related topic was readed an applyed by me.</p></li>
  </ol>
  
  <p>here is my code: </p>
  
  <pre><code>        [HttpPost]
      public ActionResult LogOn(LogOnModel model, string returnUrl)
      {
          if (ModelState.IsValid)
          {
              //  Se inicializa variables para el gestor de mensajes 
              SessionBag.Current.ErrorMessageAuto = new ArrayList();
              SessionBag.Current.ErrorMessage = null;
              SessionBag.Current.ErrorReturnTo = null;
  
              //Se verifica si el usuario es válido en el contexto de SQL Server, esto para guardar 
               //compatibilidad con el diseño de Merlin para Escritorio.
              Db_Conexion db = new Db_Conexion(model.UserName,model.Password);
              if (!db.connect())
              {
                  model.UserName = null;
                  model.Password = null;
                  SessionBag.Current.ErrorReturnTo = "link";
                  SessionBag.Current.ErrorMessage = db.ExceptionsText();
                  return View("Mensajes");
              }
              db.close();
  
              if (Membership.ValidateUser(model.UserName, model.Password))
              {
                  FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                  SessionBag.Current.UserName = model.UserName;
  
                  if (Url.IsLocalUrl(returnUrl) &amp;&amp; returnUrl.Length &gt; 1 &amp;&amp; returnUrl.StartsWith("/")
                      &amp;&amp; !returnUrl.StartsWith("//") &amp;&amp; !returnUrl.StartsWith("/\\"))
                  {
                      return Redirect(returnUrl);
                  }
                  else
                  {
                      **//HERE THE VARIABLES ARE LOST AND AN ERROR HAPPENS**
                      return RedirectToAction("menuOtbr", "Menu", new { area = "Configuracion" });
                  }
              }
              else
              {
                  ModelState.AddModelError("", "The user name or password provided is incorrect.");
              }
          }
  
          // If we got this far, something failed, redisplay form
          return View(model);
      }
  </code></pre>
  
  <p>I have the account controller at root's project and another controllers structured by areas.</p>
  
  <p><img src="https://i.stack.imgur.com/3vqLc.png" alt="Project Structure"></p>
  
  <p>Almost 10 days spent trying to solve this. Any Ideas, help would be very apreciated.</p>
  
  <p>The same error happens boot at deployment or production. </p>
  
  <p>This article <a href="https://stackoverflow.com/questions/3839894/losing-my-session-variables-what-exception-might-cause-sessions-to-be-lost/13027555">Losing my session variables - What exception might cause sessions to be lost?</a></p>
  
  <p>say about some IIS configuration. But doesn't explain exactly what things need to be configured.</p>
  
  <p>SessionBags Code</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Dynamic;
  
  namespace ParadigmaNet.Infraestructure
  {
  public sealed class SessionBag : DynamicObject
  {
      private static readonly SessionBag sessionBag;
  
      static SessionBag()
      {
          sessionBag = new SessionBag();
      }
  
      private SessionBag()
      {
      }
  
      private HttpSessionStateBase Session
      {
          get { return new HttpSessionStateWrapper(HttpContext.Current.Session); }
      }
  
      public override bool TryGetMember(GetMemberBinder binder, out object result)
      {
          result = Session[binder.Name];
          return true;
      }
  
      public override bool TrySetMember(SetMemberBinder binder, object value)
      {
          Session[binder.Name] = value;
          return true;
      }
  
      public override bool TryGetIndex(GetIndexBinder
             binder, object[] indexes, out object result)
      {
          int index = (int)indexes[0];
          result = Session[index];
          return result != null;
      }
  
      public override bool TrySetIndex(SetIndexBinder binder,
             object[] indexes, object value)
      {
          int index = (int)indexes[0];
          Session[index] = value;
          return true;
      }
  
      public static dynamic Current
      {
          get { return sessionBag; }
      }
  }
  }
  </code></pre>