I'm trying to set and read cookies in c#. I wrote these two methods:
public static void setCookie(string sCookie, string value)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[sCookie];
if (cookie == null)
cookie = new HttpCookie(sCookie);
cookie.Value = value;
cookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Request.Cookies.Add(cookie);
}
public static string getCookie(string sCookie)
{
if (HttpContext.Current.Request.Cookies[sCookie] == null)
return null;
return HttpContext.Current.Request.Cookies[sCookie].Value;
}
But I don't know why when I read the method getCookie, after calling setCookie, the collection HttpContext.Current.Request.Cookies contains always 2 elements, "__RequestVerificationToken" and "ASP.NET_SessionId", it doesn't contain my cookies...
Methods are not in any controller, just in a utils class, but I don't see any problem for that...
Can you figure out why my set method doesn't work? Thank you!
将cookie设置为Response对象,还设置cookie的路径
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.