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>密 码:<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>
后台代码:

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 }
-
博客地址:http://www.cnblogs.com/wolf-sun/
博客版权:如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。
分类:
[ASP.NET WebForm]
, [Js/Jquery]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义