title: Creating a Cookie in ASP.NET MVC
body: <p>This seems like it should be pretty straightforward. However, for the life of me, I can't seem to create a cookie in ASP.NET MVC. Currently, I have the following code:</p>
  
  <pre><code>DateTime lastActivityDate = DateTime.UtcNow;
  if (Request.Browser.Cookies)
  {
    HttpCookie lastActivityCookie = new HttpCookie(COOKIE_LAST_ACTIVITY, lastActivityDate.ToShortDateString());
    lastActivityCookie.Expires = DateTime.Now.AddMonths(-12);                    
    this.ControllerContext.HttpContext.Response.Cookies.Add(lastActivityCookie);
  }
  </code></pre>
  
  <p>I've set a breakpoint and noticed that the cookie appears to be getting added. (yes, I'm getting into the <code>Request.Browser.Cookies</code> block). I then attempt to retrieve the cookie using the following:</p>
  
  <pre><code>DateTime lastActivity = DateTime.UtcNow.AddDays(-7);        // Default to the past week
  
  HttpCookie lastActivityCookie = Request.Cookies[COOKIE_LAST_ACTIVITY];
  if (lastActivityCookie != null)
  {
    DateTime temp = DateTime.UtcNow;
    if (String.IsNullOrWhiteSpace(lastActivityCookie.Value) == false)
    {
      if (DateTime.TryParse(lastActivityCookie.Value, out temp))
        lastActivity = temp;
    }
  }
  </code></pre>
  
  <p>Unfortunately, <code>lastActivityCookie</code> is always <code>null</code>. In addition, when I look in the "Resources" tab in Chrome, I see the cookies branch, however, the cookie I'm trying to create is not listed. There are two other cookies listed though, including the .ASPXAUTH cookie. What am I doing wrong?</p>