C#多值cookie不起作用
本文关键字:不起作用 cookie 多值 | 更新日期: 2023-09-27 18:30:11
嗨,我正在以以下方式创建cookie:
HttpCookie cookie = new HttpCookie("CookieNameHere");
cookie.Values["test1"] = "Value1";
cookie.Values["test2"] = "Value2";
cookie.Values["test3"] = "Value3";
//I have also tried cookie.Values.Add("test1", "Value1");
cookie.Expires = DateTime.Now.AddDays(365d);
HttpContext.Current.Response.AppendCookie(cookie); //here I have also tried HttpContext.Current.Response.Cookies.Add(cookie);
但是当我使用以下代码读取cookie时:
HttpCookie cookie = new HttpCookie("CookieNameHere");
cookie = HttpContext.Current.Response.Cookies["CookieNameHere"];
我总是知道cookie.Values
是空的
我是不是做错了什么?
通常,您会在Response
中写入cookie,然后从随后的Requests
中读取它。
我看到你正试图从Response
中读取它——这是在同一个HTTP请求的上下文中,还是只是一个拼写错误?
尝试
HttpCookie cookie = HttpContext.Current.Request.Cookies["CookieNameHere"];
您必须在请求中请求这些Cookie。
HttpCookie cookie = Request.Cookies["CookieName"];