title: Using Cookie in Asp.Net Mvc 4
body: <p>I have web application in <em>Asp.Net MVC4</em> and I want to use <strong>cookie</strong> for user's login and logout. So my actions as follows:</p>
  
  <p><em>Login Action</em></p>
  
  <pre><code>    [HttpPost]
      public ActionResult Login(string username, string pass)
      {
          if (ModelState.IsValid)
          {
              var newUser = _userRepository.GetUserByNameAndPassword(username, pass);
              if (newUser != null)
              {
                  var json = JsonConvert.SerializeObject(newUser);
  
                  var userCookie = new HttpCookie("user", json);
                  userCookie.Expires.AddDays(365);
                  HttpContext.Response.Cookies.Add(userCookie);
  
                  return RedirectToActionPermanent("Index");
              }
          }
          return View("UserLog");
      }
  </code></pre>
  
  <p><em>LogOut Action</em></p>
  
  <pre><code>    public ActionResult UserOut()
      {
          if (Request.Cookies["user"] != null)
          {
              var user = new HttpCookie("user")
                  {
                      Expires = DateTime.Now.AddDays(-1),
                      Value = null
                  };
              Response.Cookies.Add(user);
          }
          return RedirectToActionPermanent("UserLog");
      }
  </code></pre>
  
  <p>And I use this cookie in _Loyout as follow:</p>
  
  <pre><code>@using EShop.Core
  @using Newtonsoft.Json
  @{
     var userInCookie = Request.Cookies["user"];
  }
  ...
    @if (userInCookie != null &amp;&amp; userInCookie.Value)
    {
          &lt;li&gt;&lt;a href="#"&gt;Salam&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href="@Url.Action("UserOut", "Home")"&gt;Cıxış&lt;/a&gt;&lt;/li&gt;
    }
    else
    {
          &lt;li&gt;&lt;a href="@Url.Action("UserLog", "Home")"&gt;Giriş&lt;/a&gt;&lt;/li&gt;
    }
  </code></pre>
  
  <p>But When I <em>click</em> *UserOut* action this action happen first time, but then it doesn't work. I put breakpoint for looking process but it get <em>UserLog</em> action doesn't <em>UserOut</em>. 
  My question is that where I use wrong way of cookie? What is a best way using cookie in <em>Asp.Net Mvc4</em> for this scenario ?</p>