Cookie和Session的详细使用方法

一、单级Cookie

1、创建:
方法一:

 HttpCookie cookie = new HttpCookie("UserId"); 
 cookie.Value = "蝈蝈";
 //HttpCookie cookie = new HttpCookie("UserId", "蝈蝈");    
 cookie.Expires =DateTime.Now.AddMinutes(2);
 HttpContext.Current.Response.AppendCookie(cookie);
 //HttpContext.Current.Response.Cookies.Add(cookie);

方法二:

HttpContext.Current.Response.Cookies["Pwd"].Value = "123456";
HttpContext.Current.Response.Cookies["Pwd"].Expires = DateTime.Now.AddMinutes(2); 

2、读取

if (HttpContext.Current.Request.Cookies[key] != null)
 {
    string value = HttpContext.Current.Request.Cookies[key];
}
else
{
   string value = "不存在键:" + key;
}

3、修改

if (HttpContext.Current.Request.Cookies[key] != null)
 {
     HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
     cookie.Value = value; //必须判空
     HttpContext.Current.Response.Cookies.Add(cookie);
 }

4、删除

HttpContext.Current.Request.Cookies.Remove("Pwd");

删除之后HttpContext.Current.Request.Cookies[“Pwd”]还是能读取到,设置Expires属性为过去某一时间即可起到删除的效果
5、修改过期时间

if (HttpContext.Current.Request.Cookies[key] != null)
 {
     HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
     cookie.Expires = DateTime.Now.AddMinutes(time);
     HttpContext.Current.Response.Cookies.Add(cookie);
 }

二、多级Cookie

1、创建:

HttpCookie cookie = new HttpCookie("Guo", "MainCookieValue");
//HttpCookie cookie = new HttpCookie("Guo");
//cookie.Value = "MainCookieValue";
cookie.Expires = DateTime.Now.AddMinutes(2);
cookie.Values["Age"] = "18";
cookie.Values["Name"] = "蝈蝈";
cookie.Values.Add("Phone", "18233399999");
HttpContext.Current.Response.Cookies.Add(cookie);
//string value = HttpContext.Current.Request.Cookies["Guo"].Value;
//value == MainCookieValue&Age=18&Name=蝈蝈&Phone=18233399999

2、读取

if (HttpContext.Current.Request.Cookies[key] != null)
{
    return HttpContext.Current.Request.Cookies[key][subKey] ?? 
    "不存在键:" + key + "->" + subKey;
}
else
{
    return "不存在键:" + key;
}

3、修改

if (HttpContext.Current.Request.Cookies[key] != null)
{
    HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
    cookie[subKey] = value; //存在修改,不存在添加
    HttpContext.Current.Response.Cookies.Add(cookie);
}

4、删除

HttpContext.Current.Request.Cookies["Guo"].Values.Remove("Age"); 

删除之后HttpContext.Current.Request.Cookies[“Guo”] [“Age”]或HttpContext.Current.Request.Cookies[“Guo”].Values[“Age”]
还是能读取到,设置Expires属性为过去某一时间即可起到删除的效果
5、修改过期时间
跟修改单级Cookie过期时间一样,只能修改一级Cookie的过期时间,不能修改二级Cookie的过期时间

三、 Session

1、创建:

HttpContext.Current.Session["UserID"] = "abc";
HttpContext.Current.Session["Pwd"] = "123";

2、读取

string result = "Session[" + name + "]不存在¨²";
if (HttpContext.Current.Session[name] != null)
{
    result = HttpContext.Current.Session[name].ToString();
}

3、修改

string msg = string.Empty;
if (HttpContext.Current.Session[key] != null)
{
    HttpContext.Current.Session[key] = value; //存在修改
}
else
{
    HttpContext.Current.Session[key] = value; //不存在添加
    msg = "Session[" + key + "]==null";
}
return msg;

4、删除

HttpContext.Current.Session.Remove(key);

Session删除确实有效果,删除之后不能读取到,但是通过设置TimeOut不能使Session马上过期,因为TimeOut的值必须是大于0的整数
5、设置过期时间
页面级的TimeOut优先级高于Web.Config中设置的timeout

Session.Timeout = 3;
<sessionState mode="InProc" timeout="1"></sessionState>

在ashx文件中使用Session需要引用using System.Web.SessionState;,然后实现IrequiresSessionState接口,
不然执行context.Session.TimeOut=1;时一直报“错误:Session=null”

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

changuncle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值