1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7
8 namespace RememberMe
9 {
10 public partial class Login : System.Web.UI.Page
11 {
12 protected string uName;
13 protected string pwd;
14 protected void Page_Load(object sender, EventArgs e)
15 {
16
17 if (Request.COOKIEs["user"] != null)
18 {
19 uName = Request.COOKIEs["user"].Values["n"];
20 pwd = Request.COOKIEs["user"].Values["p"];
21 }
22 if (IsPostBack)
23 {
24 string userName = Request.Form["txtName"];
25 string userPwd = Request.Form["txtPwd"];
26 if (!string.IsNullOrEmpty(Request.Form["rememberMe"]))
27 {
28 if (userName == "admin" && userPwd == "admin")
29 {
30 AlertAndRedirect("Index.aspx?n=" + userName, "登录成功");
31 HttpCOOKIE COOKIE = new HttpCOOKIE("user");
32 COOKIE["n"] = userName;
33 COOKIE["p"] = userPwd;
34 COOKIE.Expires = DateTime.Now.AddDays(7);
35 Response.COOKIEs.Add(COOKIE);
36 }
37 else
38 {
39 AlertAndRedirect("Login.aspx", "登录失败");
40 Response.COOKIEs["user"].Expires = DateTime.Now.AddDays(-1);
41 }
42 }
43 else
44 {
45 Response.COOKIEs["user"].Expires = DateTime.Now.AddDays(-1);
46 if (userName == "admin" && userPwd == "admin")
47 {
48 AlertAndRedirect("Index.aspx?n=" + userName, "登录成功");
49 }
50 else
51 {
52 AlertAndRedirect("Login.aspx", "登录失败");
53 }
54 }
55 }
56
57 }
58 private void AlertAndRedirect(string redirectURL, string msg)
59 {
60 Response.Write("");
61 }
62 }
63 }