使用cookie保存用户登录信息

写入Cookie

 HttpCookie _cookie = new HttpCookie("User");
_cookie.Values.Add("UserName", login.UserName);
_cookie.Values.Add("Password", login.Password);
Response.Cookies.Add(_cookie);

 

读取Cookie

HttpCookie _cookie = httpContext.Request.Cookies["User"];
string _userName = _cookie["UserName"];
string _password = _cookie["Password"];
 httpContext.Response.Write("用户名:"+_userName);

 

退出Cookie

HttpCookie _cookie = Request.Cookies["User"];
_cookie.Expires = DateTime.Now.AddHours(-1);
Response.Cookies.Add(_cookie);

 

posted @ 2015-05-09 10:16  lunawzh  阅读(839)  评论(0编辑  收藏  举报