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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

方法二:

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

2、读取

if (HttpContext.Current.Request.Cookies[key] != null)
 {
    string value = HttpContext.Current.Request.Cookies[key];
}
else
{
   string value = "不存在键:" + key;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3、修改

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

4、删除

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

删除之后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);
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

二、多级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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2、读取

if (HttpContext.Current.Request.Cookies[key] != null)
{
    return HttpContext.Current.Request.Cookies[key][subKey] ?? 
    "不存在键:" + key + "->" + subKey;
}
else
{
    return "不存在键:" + key;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3、修改

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

4、删除

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

删除之后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";
  • 1
  • 2

2、读取

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

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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4、删除

HttpContext.Current.Session.Remove(key);
  • 1

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

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

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

猜你喜欢

转载自blog.csdn.net/andrewniu/article/details/80169350