码迷,mamicode.com
首页 > 其他好文 > 详细

Cookie的写入、读取、清除

时间:2017-06-22 10:07:54      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:add   ring   清除   str   new   mat   text   users   用户   

 

1.写入Cookie值

string userName = context.Request.Form["u_Name"].ToString().Trim();
string pwd = context.Request.Form["u_Pwd"].ToString().Trim();

if (userName != "" && pwd != "")
{
  Users u = UsersDal.m_UserDal.GetModel(string.Format(" u_Name = ‘{0}‘ and u_Pwd = ‘{1}‘", userName, pwd));
if (u != null)
{
  //设置Cookie值,有效期为两个小时
  HttpCookie cookie = new HttpCookie("UserInfor");
  cookie.Value = userName + "," + pwd + "," + u.u_DeId;
  HttpContext.Current.Response.Cookies.Add(cookie);
  cookie.Expires = DateTime.Now.AddHours(2);

}
else
{
  infor = "用户名或密码错误,请确认";

}

}

2.获取Cookie值
  HttpCookie cookie = HttpContext.Current.Request.Cookies["UserInfor"];
  if (cookie == null)
 {
   infor = "您还未登录,请先登录";
 }
  else
 {
  string userInfor = cookie.Value;
  if (!string.IsNullOrEmpty(userInfor))
{
  string[] arr = userInfor.Split(‘,‘);
  string userName = arr[0];
  string pwd = arr[1];
  string deId = arr[2];
  Users u = UsersDal.m_UserDal.GetModel(string.Format(" u_Name = ‘{0}‘ and u_Pwd = ‘{1}‘", userName, pwd));

  ........

}

3.清除Cookie值

HttpCookie Cookie = new HttpCookie("UserInfor");
Cookie.Expires = DateTime.Now.AddHours(-2);
context.Response.Cookies.Add(Cookie);

 

Cookie的写入、读取、清除

标签:add   ring   清除   str   new   mat   text   users   用户   

原文地址:http://www.cnblogs.com/weimingxin/p/7063130.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!