我正在使用 c# 和 mvc。我正在尝试将 cookie 写入用户浏览器。但刷新浏览器后,cookie 消失了。
这是我编写 cookie 的代码:
movieCookie = new HttpCookie(cookieName);
movieCookie.Value = "test;
movieCookie.Expires = DateTime.Now.AddDays(30);
//add the cookie
HttpContext.Current.Response.Cookies.Add(movieCookie);
和一个用于读取 cookie 的:
//check if such cookie exist
HttpCookie movieCookie = null;
if (HttpContext.Current.Request.Cookies.AllKeys.Contains(cookieName))
movieCookie = HttpContext.Current.Request.Cookies[cookieName];
要补充的另一件事是,当我像这样搜索“AllKeys”时:
HttpContext.Current.Request.Cookies.AllKeys
由于某种原因,它显示了一个空字符串数组。有任何想法吗?