title: .net Cookie dissapears after session Timeout
body: <p>I have a bit of a problem handling cookies/session timeout, i need a persistent cookie to expire not at session timeout, but after. The problem is, that when the .net session dissapears after timeout, the cookie value is not being found, here's the code i use to set the cookie and to retrieve it.</p>
  
  <p>Code that sets the cookie:</p>
  
  <pre><code>HttpCookie cookiecontext = new HttpCookie("cookiecontext");
                          DateTime now = DateTime.Now;
                          cookiecontext["Context"] = "MB";
                          cookiecontext.Expires = now.AddHours(5);
                          HttpContext.Current.Response.Cookies.Add(cookiecontext);
  </code></pre>
  
  <p>Code that gets the cookie at errorout page when timeout happens (VerifyCookie() function clears the cookie after using it):</p>
  
  <pre><code>if (HttpContext.Current.Request.Cookies["cookiecontext"] != null )
                      {
                          if (HttpContext.Current.Request.Cookies["cookiecontext"]["Context"] == "MB")
                          {
                              controllerOwn = (IController)AppContextMobile.ApplicationContext.GetObject(controllerName.ToUpper());
                              VerifyCookie();
                          }
                          else
                          {
                              controllerOwn = (IController)AppContextWeb.ApplicationContext.GetObject(controllerName.ToUpper());
                          }
                      }
  </code></pre>
  
  <p>Do you have any idea why the cookie is not considered after the timeout? for me this code should have done the trick, but apparently it does not.</p>
  
  <p>Thanks in advance to everyone that reads this question.</p>
  
  <p><strong>Edit:</strong> Adding some info, im working with Asp.net Mvc 2, the idea of the code is to recognize wether if the user is in a mobile or web environment, to show him the appropiate timeout page styled for each of them, as all my variables died after timeout, cookies were my only option left, Idk why i cant use Request.cookies or response.cookies directly, it only allows me to use HttpContext.current.Response/Request.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Web;
  using System.Web.Mvc;
  using PBT.Models;
  using PBT.Models.Interfaces.Mobile.ViewEngine.Configurator;
  using PBT.ViewEngine.Configurator.Modules;
  using PBT.ViewEngine.Configurator.Modules.Utilities;
  
  namespace PBT.Controllers.Factory
  {
      public class QuieroControllerFactory : IControllerFactory
      {
          #region Members of IControllerFactory
  
          public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
          {
              try
              {
                  IController controllerOwn;
  
                  string Ruta = controllerName + "/" + GetAction(requestContext);
  
                  if (Ruta.Equals("Home/WelcomePage")) 
                  {
                      if (Convert.ToString(requestContext.HttpContext.Request.Form["Canal"]) == "MB")
                      {
                          HttpCookie CookieContext = new HttpCookie("CookieContext");
                          DateTime now = DateTime.Now;
                          CookieContext["Context"] = "MB"; //User in a mobile environment
                          CookieContext.Expires = now.AddHours(5);
                          HttpContext.Current.Response.Cookies.Add(CookieContext);
  
                          HttpContext.Current.Session.Add("Context", "MB");
                      }
                      if (Convert.ToString(requestContext.HttpContext.Request.Form["Canal"]) == "HB")
                      {
                          HttpCookie CookieContext = new HttpCookie("CookieContext");
                          DateTime now = DateTime.Now;
                          CookieContext["Context"] = "HB"; //User in a web environment
                          CookieContext.Expires = now.AddHours(5);
                          HttpContext.Current.Response.Cookies.Add(CookieContext);
  
                          HttpContext.Current.Session.Add("Context", "HB");
                      }
                  }
  
                  if (HttpContext.Current.Session["context"] != null)
                  {
                      if (HttpContext.Current.Session["context"] == "MB")
                      {
                          controllerOwn = (IController)AppContextMobile.ApplicationContext.GetObject(controllerName.ToUpper());
                      }
                      else
                      {
                         controllerOwn = (IController)AppContextWeb.ApplicationContext.GetObject(controllerName.ToUpper());
                      }
                  }
                  else
                  {
                      if (HttpContext.Current.Request.Cookies["CookieContext"] != null)
                      {
                          if (HttpContext.Current.Request.Cookies["CookieContext"]["Contexto"] == "MB")
                          {
                              controllerOwn = (IController)AppContextMobile.ApplicationContext.GetObject(controllerName.ToUpper());
                          }
                          else
                          {
                              controllerOwn = (IController)AppContextWeb.ApplicationContext.GetObject(controllerName.ToUpper());
                          }
                      }
                      else
                      {
                          controllerOwn = (IController)AppContextWeb.ApplicationContext.GetObject(controllerName.ToUpper());
                      }
                  }
  
                  return controllerOwn;
              }
              catch (Spring.Objects.Factory.NoSuchObjectDefinitionException ex)
              {
                  return null;
              }
              catch (Exception ex)
              {
                  throw;
              }
  
  
          }
  
          public void ReleaseController(IController controller)
          {
              if (controller is IDisposable)
              {
                  (controller as IDisposable).Dispose();
  
              }
              controller = null;
          }
  
          #endregion
  
          private static string GetAction(System.Web.Routing.RequestContext requestContext)
          {
              return Convert.ToString(requestContext.RouteData.Values["action"]);
          }
  
          public void VerifyCookie()
          {
              if (HttpContext.Current.Response.Cookies["CookieContext"] != null)
              {
                  HttpCookie myCookie = new HttpCookie("CookieContext");
                  myCookie.Expires = DateTime.Now.AddDays(-1d);
                  HttpContext.Current.Response.Cookies.Add(myCookie);
              }
          }
  
      }
  }
  </code></pre>