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”

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Web开发中,CookieSession是常用的技术,用于在客户端和服务器端之间存储和共享数据。Cookie是一小段文本信息,存储在客户端浏览器中,用于跟踪用户的行为和状态。而Session是在服务器端创建的一个对象,用于存储用户的会话信息。 CookieSession可以配合使用,实现更加完善的功能。一般来说,当用户第一次访问网站时,服务器会创建一个Session对象,并将Session ID存储在Cookie中,发送给客户端浏览器。客户端浏览器会保存这个Cookie,在后续的请求中携带该Cookie,以便服务器可以根据Session ID查找对应的Session对象,从而获取用户的会话信息。 通过CookieSession的配合,可以实现以下功能: 1. 跟踪用户的登录状态。当用户登录成功后,服务器可以将用户的登录信息存储在Session中,并将Session ID存储在Cookie中。当用户进行其他操作时,服务器可以根据Session ID查找对应的Session对象,从而判断用户是否已经登录。 2. 保存用户的偏好设置。当用户进行一些个性化设置时,服务器可以将这些设置信息存储在Session中,每次用户访问时都可以根据Session ID获取对应的设置信息,以实现个性化的服务。 3. 实现购物车功能。当用户选择商品加入购物车时,服务器可以将购物车信息存储在Session中,每次用户访问时都可以根据Session ID获取对应的购物车信息,以实现购物车功能。 需要注意的是,CookieSession都有一定的安全风险,因为它们都可以被恶意攻击者利用。为了保障用户的信息安全,需要在使用CookieSession时加强安全措施,例如设置Cookie的过期时间、使用HTTPS协议等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

changuncle

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

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

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

打赏作者

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

抵扣说明:

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

余额充值