title: Deserialize HttpCookie object from JSON
body: <p>For some reason HttpCookie object fails deserialization from JSON. I get this error - </p>
  
  <blockquote>
    <p>Cannot populate list type System.Web.HttpValueCollection. Path 'Values', line .., position ..</p>
  </blockquote>
  
  <p>I managed to deserialize the JSON into a custom class (<code>HttpCookieModel</code>) that doesn't have the <code>Values</code> property, and then rebuild the HttpCookie from the data.
  But isn't there a simpler way? </p>
  
  <pre><code>using Newtonsoft.Json; //  v7.0.1
  
  public JsonResult GetCookie() {
       return Json(new { Success = true, Username = model.UserName, Cookie = FormsAuthentication.GetAuthCookie(model.UserName, true) });   
  }
  
  private static void DoSomeTests()
  {
        // HttpWebRequest request....
        // Call GetCookie()
        // ...
        var httpResponse = (HttpWebResponse)request.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream(), true))
        {
             res = streamReader.ReadToEnd();
        }
        // Deserialize
        try
        {
             MyResponse mr = JsonConvert.DeserializeObject&lt;MyResponse&gt;(res);
        }
        catch (Exception ex)
        {
            string message = ex.Message;   // message: Cannot populate list type System.Web.HttpValueCollection. Path 'Values'....
        }
  
  
      public class MyResponse 
      {
          public bool Success { get; set; }
          public string Username { get; set; }
         // public HttpCookie Cookie { get; set; } // Problems deserializing Values collection.
          public HttpCookieModel Cookie { get; set; }
      }
  
     // This model works - but is there a simpler way?
      public class HttpCookieModel
      {
          public string Domain { get; set; }
          public DateTime Expires { get; set; }
          public bool HasKeys { get; set; }
          public bool HttpOnly { get; set; }
          public string Name { get; set; }
          public string Path { get; set; }
          public bool Secure { get; set; }
          public bool Shareable { get; set; }
          public string Value { get; set; }
  
          public HttpCookie ConvertToHttpCookie()
          {
              HttpCookie result           = new HttpCookie(this.Name);
              result.Domain               = this.Domain;
              result.Expires              = this.Expires;
              result.HttpOnly             = this.HttpOnly;
              result.Path                 = this.Path;
              result.Secure               = this.Secure;
              result.Shareable            = this.Shareable;
              result.Value                = this.Value;
              return result;
          }
      }
  }
  </code></pre>