cookie(免登陆)
cookie:页面
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="cookie.aspx.cs" Inherits="cookie.cookie" %> 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 </head> 10 <body> 11 <form id="form1" runat="server"> 12 <%--做免登的时候可以将注销的代码打开--%> 13 <%-- <% 14 if(Request.Cookies["userName"] == null || Request.Cookies["userPass"] == null) { 15 %>--%> 16 <table border="1" style="width: 80%; border-collapse: collapse"> 17 <tr> 18 <td>用户名:</td> 19 <td> 20 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 21 </td> 22 </tr> 23 <tr> 24 <td>密码:</td> 25 <td> 26 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 27 </td> 28 </tr> 29 <tr> 30 <td colspan="2"> 31 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="登录" /> 32 <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="cookie" /> 33 </td> 34 </tr> 35 </table> 36 <%-- 需要跳转的界面--%> 37 <%-- <% } else { 38 Response.Redirect("main.aspx"); 39 } %>--%> 40 </form> 41 </body> 42 </html>
cookie:后台
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 cookie 9 { 10 public partial class cookie : System.Web.UI.Page 11 { 12 13 14 protected void Page_Load(object sender, EventArgs e) 15 { 16 17 } 18 protected void Button1_Click(object sender, EventArgs e) 19 { 20 HttpCookie cookie = new HttpCookie("userName", Server.UrlEncode(this.TextBox1.Text)); 21 HttpCookie cookie1 = new HttpCookie("userPass", Server.UrlEncode(this.TextBox2.Text)); 22 //如果是中文账号,先Encode加密 23 cookie1.Expires = DateTime.Now.AddMinutes(1); 24 //最大时间DateTime.MaxValue; 25 cookie.Expires = DateTime.Now.AddMinutes(1); 26 Response.Cookies.Add(cookie1); 27 Response.Cookies.Add(cookie); 28 } 29 30 protected void Button2_Click(object sender, EventArgs e) 31 { 32 //Server.UrlDecode:如果是中文账号,Decode解密 33 this.TextBox1.Text = Server.UrlDecode(Request.Cookies["userName"].Value); 34 this.TextBox2.Text = Server.UrlDecode(Request.Cookies["userPass"].Value); 35 } 36 } 37 }