asp.net cookie和session的详细使用

cookie使用代码:

 //设置Cookie
            HttpCookie setCookie = new HttpCookie("test");
            setCookie.Values.Add("Name","Admin");
            Response.Cookies.Add(setCookie);
            setCookie.Expires = DateTime.Now.AddSeconds(5);

            //读取Cookie
            HttpCookie getCookie = Request.Cookies["test"];
            string name;
            if (getCookie!=null)
            {
                name = getCookie.Values["Name"];
            }

            //修改Cookie
            int counter = 0;
            if (Request.Cookies["counter"] == null)
                counter = 0;
            else
                counter = counter + 1;
            Response.Cookies["counter"].Value = counter.ToString();
            Response.Cookies["counter"].Expires = DateTime.Now.AddSeconds(10);

            //删除指定的单个Cookie(利用一个过期的Cookie来代替它)
            HttpCookie delCookie = new HttpCookie("test");
            delCookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(delCookie);

            //删除当前域的所有Cookie
            HttpCookie delCookies;
            int cookies = Request.Cookies.Count;
            for (int i = 0; i <= cookies; i++)
            {
                delCookies = Request.Cookies[i];
                delCookies.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(delCookies);
            }
session使用代码:

  //在Session里面存储一个DataSet
            System.Data.DataSet dataSet = new System.Data.DataSet();
            Session["dataSet"] = dataSet;
            //从Session里取得该DataSet
            dataSet = (System.Data.DataSet)Session["dataSet"];
"SessionID:"+Session.SessionID.ToString()+"</br>";
 "Session的数量:"+Session.Count.ToString()+"</br>";
 "Session的模式:"+Session.Mode.ToString()+"</br>";
 "Session的有效期:"+Session.Timeout.ToString()+"分钟";


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值