C#操作cookie
2020-12-13 16:00
标签:style blog http color sp div on log bs 创建Cookie 读取Cookie 修改Cookie 删除Cookie 设置Cookie过期的时间的TimeSpan C#操作cookie 标签:style blog http color sp div on log bs 原文地址:http://www.cnblogs.com/llxy/p/4078563.html HttpCookie cookie = new HttpCookie("MyCook");//初使化并设置Cookie的名称
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(1, 0, 0, 0, 0);//过期时间为1天
cookie.Expires = dt.Add(ts);//设置过期时间
cookie.Values.Add("uid", uid); //添加cookie
Response.AppendCookie(cookie);
if (Request.Cookies["MyCook"] != null)
{
ViewData["Uid"] = Request.Cookies["MyCook"]["uid"]; //输出cookie
}
//获取客户端的Cookie对象
HttpCookie cookie= Request.Cookies["MyCook"];
if (cookie!= null)
{
//修改Cookie的两种方法
cookie.Values["uid"] = "134626655";
cookie.Values.Set("userid", "134626655");
//往Cookie里加入新的内容
cookie.Values.Set("newid", "newValue");
Response.AppendCookie(cookie);
}
HttpCookie cookie= Request.Cookies["MyCook"];
if (cookie!= null)
{
if (!CheckBox1.Checked)
{
cookie.Values.Remove("uid");//移除键值为uid的值
}
else
{
TimeSpan ts = new TimeSpan(-1, 0, 0, 0);
cookie.Expires = DateTime.Now.Add(ts);//删除整个Cookie,只要把过期时间设置为现在
}
Response.AppendCookie(cookie);
}
[Serializable]
public TimeSpan(
int days,
int hours,
int minutes,
int seconds
);
上一篇:Java 日期格式化
下一篇:python中的正则表达式的使用