asp,net 读写cookie(个人笔记)
设置
if (Response.Cookies["id"] != null)
{
Response.Cookies["id"].Value = userLoginTrans.DtoOut.UserId.ToString();
Response.Cookies["id"].Expires = DateTime.Now.AddDays(1);
}
else
{
HttpCookie ck = new HttpCookie("id");
ck.Value = "10016";
ck.Expires = DateTime.Now.AddHours(24);
Response.Cookies.Add(ck);
Response.Cookies.Set(ck);
Response.Cookies["HKUserId"].Expires = DateTime.Now.AddDays(1);
}
读取
if (HttpContext.Current.Request.Cookies["id"] != null )
{
long id = Convert.ToInt64(HttpContext.Current.Request.Cookies["id"].Value);
}