- string username = this.txtUserName.Text;//用户名
- string password = this.txtPassword.Text;//密码
- if (UserManager.LoginValidate(username, password) && this.ckState.Checked == true)//判断登录名和密码是否正确和是否选择了记住用户名和密码的复选框
- {
- //判断客户端浏览器是否存在该Cookie 存在就先清除
- if (Request.Cookies["username"] != null && Request.Cookies["password"] != null)
- {
- Response.Cookies["username"].Expires = System.DateTime.Now.AddSeconds(-1);//Expires过期时间
- Response.Cookies["password"].Expires = System.DateTime.Now.AddSeconds(-1);
- }
- else
- {
- //向客户端浏览器加入Cookie (用户名和密码 最好是使用MD5加密)
- HttpCookie hcUserName1 = new HttpCookie("username");
- hcUserName1.Expires = System.DateTime.Now.AddDays(7);
- hcUserName1.Value = username;
- HttpCookie hcPassword1 = new HttpCookie("password");
- hcPassword1.Expires = System.DateTime.Now.AddDays(7);
- hcPassword1.Value = password;
- Response.Cookies.Add(hcUserName1);
- Response.Cookies.Add(hcPassword1);
- }
- }
코드 복사 로그인 후 페이지로 가------------------------ (마스터 페이지와 메인보드 페이지를 함께 작성하는 것이 가장 좋습니다------------
- if (Request.Cookies["username"] != null && Request.Cookies["password"] != null)
- {
- //用户曾登录
-
- username = Request.Cookies["username"].Value.ToString(); //读取Cookie
- password = Request.Cookies["password"].Value.ToString();//判断Cookie读取出来的用户名和密码是否能正确登录
- if (UserManager.LoginValidate(username, password))
- {
- //登录后的代码
- } }
코드 복사
쿠키 ------------------------------- 옵트아웃(그리고 옵트아웃 버튼)도 있습니다------------------------------- 출구
- HttpCookie hcUserName1 = new HttpCookie("username");
- hcUserName1.Expires = System.DateTime.Now.AddDays(-7);
- hcUserName1.Value = username;
- HttpCookie hcPassword1 = new HttpCookie("password");
- hcPassword1.Expires = System.DateTime.Now.AddDays(-7);
- hcPassword1.Value = password;
- Response.Cookies.Add(hcUserName1);
- Response.Cookies.Add(hcPassword1);
코드 복사
|