[英]Cookies become null when page is loaded
I have two asp.net pages.我有两个 asp.net 页。 I set cookies using following code in Login Page.我在登录页面中使用以下代码设置了 cookies。
HttpCookie cookie = new HttpCookie("sample");
cookie.Values.Add(cookieValues);
cookie.Expires = DateTime.Now.AddMinutes(60);
HttpContext.Current.Response.Cookies.Add(cookie);
Cookie is set successfully with expired date.已成功设置 Cookie,但已过期。 I can see it on Watch window of Visual Studio.我可以在 Visual Studio 的 Watch window 上看到它。
However, when I tried to look for the values in another page during page load, both request and response cookies are null.但是,当我在页面加载期间尝试在另一个页面中查找值时,请求和响应 cookies 都是 null。
HttpCookie respCookie = HttpContext.Current.Request.Cookies["sample"];
if (respCookie != null)
{
DateTime expDate = respCookie.Expires;
if (expDate > DateTime.Now)
return respCookie;
else
return null;
}
else
return null;
Try disabling browser extensions or running your page in anonymous mode.尝试禁用浏览器扩展或以匿名模式运行您的页面。
I was using Avast browser extension which messed with my cookies, worked for me.我正在使用 Avast 浏览器扩展,它与我的 cookies 混淆,对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.