asp.net使用cookies记录登录状态
2014-07-25 13:11
399 查看
在 ----------------------Default.aspx
在----------------Default.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <table> <tr> <td style="width: 15%; text-align: right;"> User Name </td> <td> <asp:TextBox ID="TextBoxUserName" runat="server"></asp:TextBox> </td> </tr> <tr> <td style="text-align: right;"> Password </td> <td> <asp:TextBox ID="TextBoxPassword" TextMode="Password" runat="server"></asp:TextBox> </td> </tr> <tr> <td style="text-align: right;"> Remember me </td> <td> <asp:CheckBox ID="CheckBoxRememberMe" runat="server" /> </td> </tr> <tr> <td style="text-align: right;"> </td> <td> <asp:Button ID="ButtonLogin" runat="server" Text="Login" OnClick="ButtonLogin_Click" /> </td> </tr> </table> </form> </body> </html>
在----------------Default.aspx.cs
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; namespace WebApplication3 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.Cookies["Name"] != null && Request.Cookies["Password"] != null) { this.TextBoxUserName.Text = Request.Cookies["Name"].Value; this.TextBoxPassword.Attributes["value"] = Request.Cookies["Password"].Value; } } } protected void ButtonLogin_Click(object sender, EventArgs e) { Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1); Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1); if (CheckBoxRememberMe.Checked) { Response.Cookies["Name"].Expires = DateTime.Now.AddDays(7); Response.Cookies["Password"].Expires = DateTime.Now.AddDays(7); } Response.Cookies["Name"].Value = this.TextBoxUserName.Text.Trim(); Response.Cookies["Password"].Value = this.TextBoxPassword.Text.Trim(); } } }
相关文章推荐
- Asp.Net使用加密cookie代替session验证用户登录状态 源码分享
- asp.net cookie 实现 记录用户登录状态,下次自动登录
- asp.net 使用cookies或者session实现浏览历史记录功能
- ASP.NET MVC 使用Filter过滤器 验证用户登录状态
- Asp.Net使用加密cookie代替session验证用户登录状态 源码分享 欢迎拍砖
- ASP.NET使用Cookies保存登录信息
- asp.net(存储过程)使用三种方法存取数据库记录(20070510)
- Scott Mitchell 的ASP.NET 2.0数据教程之三十一::使用DataList来一行显示多条记录
- Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
- 今天在使用C#的ASP.NET给Access的数据库中添加新记录发现的问题
- Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
- 在ASP.NET Server Control中使用控件状态
- Scott Mitchell 的ASP.NET 2.0数据教程之三十一::使用DataList来一行显示多条记录
- 记录:在ASP.NET中使用ActiveX插件时遇到的问题
- Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
- asp.net中application,cookies,stateview,session的使用
- ASP.NET 2.0中用Gridview控件操作数据——使用Gridview插入新记录
- 在ASP.NET中实现单点登录(不允许多用户使用同一帐号同时登录)
- ASP.NET页面状态管理——ViewState的使用
- Scott Mitchell 的ASP.NET 2.0数据教程之三十一::使用DataList来一行显示多条记录