【问题标题】:Convert a Cookie object in to string format and back将 Cookie 对象转换为字符串格式并返回
【发布时间】:2011-08-12 23:26:32
【问题描述】:

如何将 cookie/cookie 集合转换为其字符串表示形式? (在 ASP.Net 中)

我要找的是

cookie-collection  => "name1=value1 expires=date1; name2=value2 path=/test"

反之亦然。

【问题讨论】:

  • 你已经走了多远?
  • 我有一些类似于@Stecya 发布的内容。我正在寻找一个可以帮助我做到这一点的图书馆。我很惊讶基础框架还没有公开这个!

标签: c# asp.net serialization cookies


【解决方案1】:

你在寻找这样的东西吗?

   //Convert to string
   HttpCookieCollection source = new HttpCookieCollection();
   string result = source.Cast<HttpCookie>().
                   Aggregate(string.Empty, (current, cookie) => 
                   current + string.Format("{0}={1} ", cookie.Name, cookie.Value));


   //Convert back to collection
   HttpCookieCollection dest = new HttpCookieCollection();
   foreach (var pair in result.Split(' '))
   {
        string[] cookies = pair.Split('=');
        dest.Add(new HttpCookie(cookies[0],cookies[1]));
   }

【讨论】:

  • 我现在有类似的东西。但它目前不处理过期、路径、域等参数。我正在寻找一些我可以使用的库,而不是滚动我自己的代码,如果我可以避免的话。
猜你喜欢
  • 2011-09-09
  • 2015-09-24
  • 2021-09-19
  • 1970-01-01
  • 2019-12-17
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多