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);