坐峰怀雪灬

路漫漫其修远兮,吾将上下而求索。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#写入登陆Cookies

Posted on 2016-06-29 16:23  坐峰怀雪灬  阅读(350)  评论(0编辑  收藏  举报
 1 protected void Page_Load(object sender, EventArgs e)
 2         {
 3             //打开登录页面时获取客户端cookie值并写入前台控件中
 4             HttpCookie cookie = Request.Cookies["name"];
 5             if (cookie == null)
 6             {
 7                 UserName.Text = "";
 8  
 9             }
10             else
11             {
12                 UserName.Text = cookie.Value;
13             }
14         }
15  
16         protected void btnSubmit_Click(object sender, EventArgs e)
17         {
18             string username = Request.QueryString["UserName"];
19             string password = Request.QueryString["PassWord"];
20             if (登录成功)
21             {
22                 Response.Write("登陆成功");
23                 HttpCookie cookie=new HttpCookie("name",username);//获取用户的用户名
24                 cookie.Expires = DateTime.Now.AddDays(10);//设置cookie过期时间为10天后
25                 Response.Cookies.Add(cookie);//将cookie写入客户端
26             }
27             else
28             {
29                 Response.Write("登陆失败");
30             }
31         }