Cookie对象的应用

Cookie应用

在http协议中,Cookie是一个文本文件,它是服务器或脚本,用户维护用户信息的一种方式。
Cookie集合是Request对象和Response对象,经常用到的集合。
Cookie包含会话Cookie和永久Cookie两种。
会话Cookie——是临时性的,它只在浏览器打开时存在。
永久Cookie——则永久的保存在客户机上,并且,在指定过期日期之前均可用。
如果没有给Cookie设置过期日期,它将自动成为一个会话Cookie,如果设置,则会成为一个永久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实现密码记忆</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">
                    密&nbsp;&nbsp;&nbsp; 码:</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" />
&nbsp;
                    <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)
        {
            //默认设置用户名、密码都为admin
            if (txtname.Text.Trim().Equals("admin") && txtpwd.Text.Trim().Equals("admin"))
            {
                Session["username"] = txtname.Text.Trim();
                //如果选择记住密码
                if (ckbauto.Checked)
                {
                    //判断admin是否已经存在
                    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)
        {
            //当用户名不为空
            if (Request.Cookies["username"] != null)
            {
                //判断输入的用户名,是否为admin
                if (Request.Cookies["username"].Value.Equals(txtname.Text.Trim()))
                {
                    //自动补全密码
                    txtpwd.Attributes["value"] = Request.Cookies["userpwd"].Value;
                }
                else
                {
                    txtpwd.Attributes["value"] = "";
                }
            }
        }
    }
}

当用户第一次输入用户名和密码admin,选择记住密码,登录之后,第二次登录时,输入用户名如果用户名存在,密码就会自动显示。

运行

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值