ASP.NET实现Cookie功能的三个基本操作(写入,读取,删除)

您所在的位置:网站首页 怎么删除cookie中的重要信息 ASP.NET实现Cookie功能的三个基本操作(写入,读取,删除)

ASP.NET实现Cookie功能的三个基本操作(写入,读取,删除)

2024-07-12 15:09:30| 来源: 网络整理| 查看: 265

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;

public partial class Test : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) {

} protected void WriteClicked(object sender, EventArgs e) { //创建一个HttpCookie对象 HttpCookie cookie = new HttpCookie("jsjx"); //设定此cookies值 cookie.Value = "GDUFS的值"; //设定cookie的生命周期,在这里定义为一个小时 DateTime dtNow = DateTime.Now; TimeSpan tsMinute = new TimeSpan(0, 0, 3, 0); cookie.Expires = dtNow + tsMinute; cookie["姓名"] = "王天"; cookie["性别"] = "男"; cookie["年龄"] = "26"; //加入此cookie Response.Cookies.Add(cookie); Response.Write(NameField.Text + "Cookie创建完毕 "); } protected void ReadClicked(Object Sender, EventArgs e) { //得到用户输入的cookie名称 String strCookieName = "11"; //获得cookie HttpCookie cookie = Request.Cookies[strCookieName]; //确定是否存在用户输入的cookie if (null == cookie) { Response.Write("没有发现指定的cookie "); } else { //找到指定的cookie,显示cookie的值 String strCookieValue = cookie.Value.ToString(); Response.Write(strCookieName + " cookie 的值为: " + strCookieValue + " "); Response.Write(cookie["姓名"]); } } protected void Button3_Click(object sender, EventArgs e) { System.Text.StringBuilder output = new System.Text.StringBuilder(); HttpCookie aCookie;

//循环输出Cookie的内容(1) //for (int i = 0; i < Request.Cookies.Count; i++) //{ // aCookie = Request.Cookies[i]; // output.Append("Cookie name = " + Server.HtmlEncode(aCookie.Name) // + ""); // output.Append("Cookie value = " + Server.HtmlEncode(aCookie.Value) // + ""); //} //Label1.Text = output.ToString();

//循环输出Cookie的内容(2) //for (int i = 0; i < Request.Cookies.Count; i++) //{ // Response.Write("Cookie[" + i + "]的Name为:" + Request.Cookies[i].Name + ""); // Response.Write("Cookie[" + i + "]的Value为:" + Request.Cookies[i].Value + ""); //}

//显示子键(1) //for (int i = 0; i < Request.Cookies.Count; i++) //{ // aCookie = Request.Cookies[i]; // output.Append("Name = " + aCookie.Name + ""); // if (aCookie.HasKeys) // { // for (int j = 0; j < aCookie.Values.Count; j++) // { // string subkeyName = Server.HtmlEncode(aCookie.Values.AllKeys[j]); // string subkeyValue = Server.HtmlEncode(aCookie.Values[j]); // output.Append("Subkey name = " + subkeyName + ""); // output.Append("Subkey value = " + subkeyValue + // ""); // } // } // else // { // output.Append("Value = " + Server.HtmlEncode(aCookie.Value) + // ""); // } //} //Label1.Text = output.ToString();

////显示子键(2) //for (int i = 0; i < Request.Cookies.Count; i++) //{ // Response.Write("Cookie[" + i + "]的Name为:" + Request.Cookies[i].Name + ""); // if (Request.Cookies[i].HasKeys) // { // for (int j = 0; j < Request.Cookies[i].Values.Count; j++) // { // Response.Write("Cookie[" + i + "][" + j + "]的Name为:" + Request.Cookies[i].Values.AllKeys[j] + ""); // Response.Write("Cookie[" + i + "][" + j + "]的Value为:" + Request.Cookies[i].Values[j] + ""); // } // } //}

//for (int i = 0; i < Request.Cookies.Count; i++) //{ // aCookie = Request.Cookies[i]; // output.Append("Name = " + aCookie.Name + ""); // if (aCookie.HasKeys) // { // for (int j = 0; j < aCookie.Values.Count; j++) // { // subkeyName = Server.HtmlEncode(aCookie.Values.AllKeys[j]); // subkeyValue = Server.HtmlEncode(aCookie.Values[j]); // output.Append("Subkey name = " + subkeyName + ""); // output.Append("Subkey value = " + subkeyValue + // ""); // } // } // else // { // output.Append("Value = " + Server.HtmlEncode(aCookie.Value) + // ""); // } //} //Label1.Text = output.ToString();

//删除全部Cookie string cookieName; int limit = Request.Cookies.Count; for (int i = 0; i < limit; i++) { cookieName = Request.Cookies[i].Name; aCookie = new HttpCookie(cookieName); aCookie.Expires = DateTime.Now.AddDays(-1); Response.Cookies.Add(aCookie); }

//删除全部Cookie for (int i = 0; i < Request.Cookies.Count; i++) { string cookiesName = Request.Cookies[i].Name; HttpCookie cookie = new HttpCookie(cookiesName); cookie.Expires = DateTime.Now.AddDays(-1); Response.Cookies.Add(cookie);

} }}



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭