您好我正在通过以下方式创建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
是空的
我在这里做错了吗?
答案 0 :(得分:2)
通常您会在Response
中编写Cookie,然后从后续Requests
中读取。
我看到你试图从Response
读取它 - 这是在同一个HTTP请求的上下文中,还是只是一个错字?
尝试
HttpCookie cookie = HttpContext.Current.Request.Cookies["CookieNameHere"];
答案 1 :(得分:1)
您必须在请求中询问这些Cookie。
HttpCookie cookie = Request.Cookies["CookieName"];