当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

asp.net的cookie的读写实例

  先演示简单的cookie读写操作,下次加入des加密解密,敬请期待!

  1HttpCookie cookie = new HttpCookie("AutoLogin");

  2                cookie.Values.Add("name", user.UserName);

  3                cookie.Values.Add("pass", user.Password);

  4                cookie.Expires = DateTime.Now.AddDays(1);

  5                Response.AppendCookie(cookie);

 

  1HttpCookie cookies = Request.Cookies["AutoLogin"];

  2        if (!IsPostBack)

  3        {

  4            if (cookies != null)

  5            {

  6                string name = cookies.Values["name"];

  7                string pass = cookies.Values["pass"];

  8                if (name != null && !name.Equals("") && pass != null && !pass.Equals(""))

  9                {

  10                    UserManager userManager = new UserManager();

  11                    if (userManager.Login(name, pass, out user))

  12                    {

  13                        limits = userManager.GetLimitsById(user.ID);

  14                        Session["limits"] = limits;

  15                        Session["CurrentUser"] = user;

  16                        Response.Redirect("~/Display/Car/CarList.aspx");

  17                    }

  18                }

  19            }

  20        }

  清空cookies

  Response.Cookies["AutoLogin"].Expires = DateTime.Now.AddDays(-1);

相关内容
赞助商链接