title: MVC 4 - Cookie already expired on ActionExecutingContext
body: <p>I'm a little lost on this and I can't find a solution.</p>
  
  <p>I'm creating a Filter using below code:</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
      public class SessionFilter :System.Web.Mvc.ActionFilterAttribute
      {
          public override void OnActionExecuting(ActionExecutingContext filterContext)
          {
  </code></pre>
  
  <p>And I'm validating if a cookie exist, is null or blank or expired with below code:</p>
  
  <pre><code>if (filterContext.HttpContext.Request.Cookies["MyCookie"] == null ||
      String.IsNullOrEmpty(filterContext.HttpContext.Request.Cookies["MyCookie"].Value) ||
      (filterContext.HttpContext.Request.Cookies["MyCookie"].Expires &lt; DateTime.Now &amp;&amp;
      filterContext.HttpContext.Request.Cookies["MyCookie"].Expires != new DateTime()))
                     {
  </code></pre>
  
  <p>But cookie is already expired in this section even when the value for expiration is set for one day after:</p>
  
  <pre><code>private void SetAuthenticationCookie(String username, String MyValue)
  {
      FormsAuthentication.SetAuthCookie(username.ToUpper(), false);
      var cookie = FormsAuthentication.GetAuthCookie(username, false);
      var ticket = FormsAuthentication.Decrypt(cookie.Value);
  
      HttpCookie newCookie = new HttpCookie("MyCookie", MyValue);
      newCookie.Expires = ticket.Expiration;
      HttpContext.Current.Response.Cookies.Add(newCookie);
  }
  </code></pre>
  
  <p>Here is the configuration on Web.config:</p>
  
  <pre><code>&lt;authentication mode="Forms"&gt;
    &lt;forms loginUrl="~/Login/SignIn/" protection="All" timeout="1440" name=".ASPXAUTH" path="/" requireSSL="false" slidingExpiration="true" defaultUrl="/" cookieless="UseDeviceProfile" enableCrossAppRedirects="false" /&gt;
  &lt;/authentication&gt;
  </code></pre>
  
  <p>Thank you so much again.</p>