首页 > 代码库 > Cookie

Cookie

cookie的使用:

1.首先,在注册数据插入到数据库后准备返回视图是写入cookie

 //写cookie
                            HttpCookie cookie = new HttpCookie("LoginU");//定义cookie
                            cookie.Values["UN"] = model.Username;//记录用户名
                            cookie.Values["PW"] = model.Password;//记录密码
                            cookie.Expires = DateTime.Now.AddDays(15);//设置过期时间
                            Response.Cookies.Add(cookie);//向浏览器写入cookie

2.在登录的时候使用cookie,将已经写入到浏览器的cookie值填充到登录框中(对于password类型的需要JavaScript对input框value值的填充)

var model = new LoginModel();
            //使用cookie
            HttpCookie cookie = Request.Cookies["LoginU"];//使用cookie
            if (cookie != null)
            {
                model.Username = cookie.Values["UN"].ToString();
                model.Password = cookie.Values["PW"].ToString();
            }

 

Cookie