cookie(免登錄)

cookie:頁面html

 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>
View Code

cookie:後臺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 }
View Code
相關文章
相關標籤/搜索