asp.net cookie的读写实例
作者:
先演示简单的cookie读写操作,下次加入des加密解密,敬请期待!
asp.net操作cookie的例子,包括创建cookie、读取cookie、清空cookies,有需要的朋友可以参考下。
1、创建cookie
HttpCookie cookie = new HttpCookie("AutoLogin");
cookie.Values.Add("name", user.UserName);
cookie.Values.Add("pass", user.Password);
cookie.Expires = DateTime.Now.AddDays(1);
Response.AppendCookie(cookie);
2、读取cookie
HttpCookie cookies = Request.Cookies["AutoLogin"];
if (!IsPostBack)
{
if (cookies != null)
{
string name = cookies.Values["name"];
string pass = cookies.Values["pass"];
if (name != null && !name.Equals("") && pass != null && !pass.Equals(""))
{
UserManager userManager = new UserManager();
if (userManager.Login(name, pass, out user))
{
limits = userManager.GetLimitsById(user.ID);
Session["limits"] = limits;
Session["CurrentUser"] = user;
Response.Redirect("~/Display/Car/CarList.aspx");
}
}
}
}
3、清空cookies
Response.Cookies["AutoLogin"].Expires = DateTime.Now.AddDays(-1);
1、创建cookie
复制代码 代码如下:
HttpCookie cookie = new HttpCookie("AutoLogin");
cookie.Values.Add("name", user.UserName);
cookie.Values.Add("pass", user.Password);
cookie.Expires = DateTime.Now.AddDays(1);
Response.AppendCookie(cookie);
2、读取cookie
复制代码 代码如下:
HttpCookie cookies = Request.Cookies["AutoLogin"];
if (!IsPostBack)
{
if (cookies != null)
{
string name = cookies.Values["name"];
string pass = cookies.Values["pass"];
if (name != null && !name.Equals("") && pass != null && !pass.Equals(""))
{
UserManager userManager = new UserManager();
if (userManager.Login(name, pass, out user))
{
limits = userManager.GetLimitsById(user.ID);
Session["limits"] = limits;
Session["CurrentUser"] = user;
Response.Redirect("~/Display/Car/CarList.aspx");
}
}
}
}
3、清空cookies
复制代码 代码如下:
Response.Cookies["AutoLogin"].Expires = DateTime.Now.AddDays(-1);