Reputation: 284
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!
Upvotes: 2
Views: 4854