Cookie对象的应用
来源:互联网 发布:小米手环好用吗 知乎 编辑:程序博客网 时间:2024/06/05 15:11
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"> 密 码:</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) { //默认设置用户名、密码都为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
- Cookie对象的应用
- cookie对象及其应用:
- JSP应用开发(JSP+JavaBean)---Cookie对象的应用实例
- asp对象cookie应用集合
- asp对象cookie应用集合
- asp对象cookie应用集合
- asp对象cookie应用集合
- cookie对象的简介
- cookie对象的使用
- Cookie 对象的使用
- 简单cookie的应用
- Cookie的实例应用
- Cookie的实例应用
- Cookie的实例应用
- Cookie的实例应用
- Cookie的实例应用
- Cookie的应用
- COOKIE的应用
- 基于FFMPEG的音频解码器
- Apache Commons-logging使用实例
- 如何选用 vector 和 list
- Loadrunner 结果分析
- 为什么在Python里推荐使用多进程而不是多线程?
- Cookie对象的应用
- Android在布局中动态添加view的两种方法
- 安卓开源项目周报0426
- HTTP和HTTPS详解
- oj3117复杂类型作函数参数之一维数组作函数参数
- Linux 服务器时间同步设置
- H264视频码流分解为NALU数据代码
- wamp下 php连接oracle数据库
- gRPC vs Thrift