Cookie对象的应用
生活随笔
收集整理的這篇文章主要介紹了
Cookie对象的应用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Cookie應(yīng)用
在http協(xié)議中,Cookie是一個文本文件,它是服務(wù)器或腳本,用戶維護用戶信息的一種方式。
Cookie集合是Request對象和Response對象,經(jīng)常用到的集合。
Cookie包含會話Cookie和永久Cookie兩種。
會話Cookie——是臨時性的,它只在瀏覽器打開時存在。
永久Cookie——則永久的保存在客戶機上,并且,在指定過期日期之前均可用。
如果沒有給Cookie設(shè)置過期日期,它將自動成為一個會話Cookie,如果設(shè)置,則會成為一個永久Cookie。
WebForm1.aspx代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"><title>利用Cookie實現(xiàn)密碼記憶</title><style type="text/css">.style1{width: 200;float: left;height: 92px;}.style2{text-align: left;}.style3{text-align: left;height: 33px;}.style4{height: 33px;}#form1{height: 114px;}</style> </head> <body><form id="form1" runat="server"><table align="left" cellpadding="0" cellspacing="0" class="style1" style="font-size: small"><tr><td class="style2">用戶名:</td><td><asp:TextBox ID="txtname" runat="server" Width="120px" AutoPostBack="True" ontextchanged="txtname_TextChanged"></asp:TextBox></td></tr><tr><td class="style2">密 碼:</td><td><asp:TextBox ID="txtpwd" runat="server" TextMode="Password" Width="120px"></asp:TextBox></td></tr><tr><td class="style3"></td><td class="style4"><asp:CheckBox ID="ckbauto" runat="server" Text="記住密碼" /></td></tr><tr><td class="style2" colspan="2"><asp:Button ID="Button1" runat="server" Text="登錄" onclick="Button1_Click" /> <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="重置" /></td></tr><tr><td class="style2" colspan="2"><br /><asp:Label ID="Label1" runat="server" Text=""></asp:Label></td></tr></table> <br /></form> </body> </html>WebForm1.aspx.cs代碼
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;namespace WebApplication1 {public partial class WebForm1 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}//重置protected void Button2_Click(object sender, EventArgs e){txtname.Text = "";txtpwd.Text = "";Label1.Text = "";}//登錄protected void Button1_Click(object sender, EventArgs e){//默認(rèn)設(shè)置用戶名、密碼都為adminif (txtname.Text.Trim().Equals("admin") && txtpwd.Text.Trim().Equals("admin")){Session["username"] = txtname.Text.Trim();//如果選擇記住密碼if (ckbauto.Checked){//判斷admin是否已經(jīng)存在if (Request.Cookies["username"] == null){Response.Cookies["username"].Expires = DateTime.Now.AddDays(30);Response.Cookies["userpwd"].Expires = DateTime.Now.AddDays(30);Response.Cookies["username"].Value = txtname.Text.Trim();Response.Cookies["userpwd"].Value = txtpwd.Text.Trim();}}Label1.Text = "登錄成功";}else{ClientScript.RegisterStartupScript(this.GetType(), "", "alert('用戶名或密碼錯誤!');", true);}}//用戶名改變時protected void txtname_TextChanged(object sender, EventArgs e){//當(dāng)用戶名不為空if (Request.Cookies["username"] != null){//判斷輸入的用戶名,是否為adminif (Request.Cookies["username"].Value.Equals(txtname.Text.Trim())){//自動補全密碼txtpwd.Attributes["value"] = Request.Cookies["userpwd"].Value;}else{txtpwd.Attributes["value"] = "";}}}} }當(dāng)用戶第一次輸入用戶名和密碼admin,選擇記住密碼,登錄之后,第二次登錄時,輸入用戶名如果用戶名存在,密碼就會自動顯示。
運行
總結(jié)
以上是生活随笔為你收集整理的Cookie对象的应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Cookie概述
- 下一篇: 打开aspx现有实例