.net对Cookie的简单操作

时间:2021-11-02 06:57:48

1 声明:HttpCookie MyCookie= new HttpCookie("test");

2增加:MyCookie.Values.Add("key1", "value1");//key1是键名,value1是值

3过期:MyCookie.Expires = DateTime.Now.AddDays(1);//一天之后过期。也可以设置一个负值,让Cookie立即失效

4添加到客户端:Response.AppendCookie(NewCookies);//用Response添加

5取值:

5.1  实例化:HttpCookie TestCookie = Request.Cookies["test"];//用Request取值,判断TestCookie为不为空,检测名为test的Cookie存不存在

5.2  取值:string CookieStr = TestCookie.Value;//获取单个Cookie值

string key1Str = TestCookie.Values["key1"]//获取名为test的Cookie的键名为key1的值