我有来自两台不同机器的两个浏览器进入测试页面。测试页面正在检索cookie。如果没有,它会创建一个cookie。
public string GetUserCookieId()
{
string cookieName = "CookieId3";
HttpCookie userInfoCookies = Request.Cookies[cookieName];
string cookieId = "";
if (userInfoCookies != null)
cookieId = userInfoCookies.Value;
if (string.IsNullOrEmpty(cookieId))
{
cookieId = Guid.NewGuid().ToString();
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Value = cookieId;
cookie.Expires = DateTime.Now.AddDays(90);
Response.SetCookie(cookie);
}
return cookieId;
}
两台浏览器计算机都在页面上显示相同的Cookie值。这甚至一旦我将cookie重命名为“CookieId3”。请告诉我我哪里出错了。
如您所见,该方法不是静态的。谢谢
答案 0 :(得分:0)
当从ClassLibrary访问代码时,问题似乎是Request和HttpContext.Current.Request之间的区别。
有人能够对此有所了解吗?!
以下作品。
public string GetUserCookieId()
{
string cookieName = "CookieId18";
HttpCookie userInfoCookies = HttpContext.Current.Request.Cookies[cookieName];
string cookieId = "";
if (userInfoCookies != null)
cookieId = userInfoCookies.Value;
if (string.IsNullOrEmpty(cookieId))
{
cookieId = Guid.NewGuid().ToString();
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Value = cookieId;
cookie.Expires = DateTime.Now.AddDays(90);
HttpContext.Current.Response.SetCookie(cookie);
}
return cookieId;
}