title: Unable to update cookies in asp.net mvc
body: <p>I can write and read cookies but I can't change value for existing cookie it always has first set value. I found few ways how it can be implemented but no one works. Here is my code:</p>
  
  <pre><code>private void AddPost(string key)
      {
          var context = System.Web.HttpContext.Current;
          var request = context.Request;
          var response = context.Response;
  
          var cookie = request.Cookies[Constants.PostsViewing];
  
          if (cookie == null || string.IsNullOrEmpty(cookie.Value))
          {
              response.Cookies.Add(new HttpCookie(Constants.PostsViewing, key)
              {
                  Expires = DateTime.Now.AddDays(365)
              });
          }
          else
          {
              if (cookie.Value.Split(';').Contains(key))
              {
                  return;
              }
  
              var v = cookie.Value + ";" + key;
  
              cookie.Value = v;
              cookie.Expires = DateTime.Now.AddDays(365);
              response.Cookies.Add(cookie);
  
              // this way also doesn't work
              //cookie.Value = v;
              //response.AppendCookie(cookie);
  
              // and this
              //response.Cookies[Constants.PostsViewing].Value = v;
              //response.Cookies[Constants.PostsViewing].Expires = DateTime.Now.AddDays(365);
          }
  
      }
  </code></pre>
  
  <p>According to <a href="http://msdn.microsoft.com/en-us/library/ms178194.aspx" rel="noreferrer">msdn</a> cookie file should be owerwritten. </p>
  
  <blockquote>
    <p>Each cookie must have a unique name so that it can be identified later when reading it from the browser. Because cookies are stored by name, naming two cookies the same will cause one to be overwritten.</p>
  </blockquote>
  
  <p>Do you have any idea how to fix it?</p>