asp.net记住我功能

登录页面的记住我功能  
不能用session的原因:sessionID是以cookie的形式存在浏览器端的内存中  如果用户把浏览器关闭 则sessionID就消失    
但是服务器端的session在过期时间内还是存在的 等到浏览器在 默认的过期时间内(20分钟)不在向服务器发送请求 则过了20分钟 session销毁!
前端简单模拟:
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="RememberMe.Login" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title></title>
 9     <script type="text/javascript">
10         window.onload = function () {
11             document.getElementById('btnClose').onclick = function () {
12                 window.close();
13             };
14         };
15     </script>
16 </head>
17 <body>
18     <form id="form1" runat="server">
19         <div style="text-align: center;">
20             <table>
21                 <tr>
22                     <td>用户名:
23                         <input type="text" name="txtName" value="<%=uName %>" /></td>
24                 </tr>
25                 <tr>
26                     <td>&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="txtPwd" value="<%=pwd %>" />
27                     </td>
28                 </tr>
29                 <tr>
30                     <td colspan="2">
31                         <input type="checkbox" name="rememberMe" value="1" checked="checked" />记住我</td>
32                 </tr>
33                 <tr>
34                     <td colspan="2">
35                         <input type="submit" value="登录" />
36                         <input type="button" value="关闭" id="btnClose" /></td>
37                 </tr>
38             </table>
39 
40         </div>
41     </form>
42 </body>
43 </html>
Login.aspx

后台代码:

 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("<script>alert('" + msg + "');window.location.href='" + redirectURL + "';</script>");
61         }
62     }
63 }
Login.aspx,cs


基本功能实现。下载:http://www.cnblogs.com/wolf-sun/admin/Files.aspx

posted @ 2013-07-26 17:23  wolfy  阅读(962)  评论(1编辑  收藏  举报