ASP.NET中怎么对cookie进行操作
本篇文章给大家分享的是有关ASP.NET中怎么对cookie进行操作,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、重庆小程序开发公司、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了揭东免费建站欢迎大家使用!
最基本的ASP.NET cookie操作:
HttpCookie cookie1 = new HttpCookie("user", "liuliang"); cookie1["age"] = "18"; cookie1.Values.Add("sex", "男"); cookie1.Expires = DateTime.Now.AddHours(1); Response.AppendCookie(cookie1); HttpCookie cookie = new HttpCookie("luis"); cookie.Values.Add("name", "dangdang"); cookie["age"] = "23"; cookie["sex"] = "man"; cookie.Expires = DateTime.Now.AddMinutes(0.5); Response.Cookies.Add(cookie);
然后读取:
HttpCookie cookie = Request.Cookies["luis"]; if (null == cookie) return; Response.Write(cookie.Value); Response.Write("< br/>age" + cookie.Values.Count.ToString()); Response.Write("< br/>age" + cookie["name"]); Response.Write("< br/>age" + cookie["age"]); Response.Write("< br/>sex" + cookie.Values["sex"]); Response.Write("< br/>sex" + cookie.Values[1]);
由于在服务器端我们不能直接删除cooike 所以只有设其过期
//删除cookie HttpCookie cookie2 = Request.Cookies["user"]; if (null == cookie2) return; cookie2.Expires = DateTime.Now.AddDays(-1); Response.AppendCookie(cookie2);
然后我们可以:
进行客户端来显示cookie
最简单的代码是:
var c=document.cookie.toString(); alert(c);
ASP.NET cookie操作方法二:还可以用cookie类操作:
下面是转自某人的操作类:
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns="http://www.w3.org/1999/xhtml"> < head> < title>无标题页< /title> < /head> < body> < script language="JavaScript" type="text/javascript"> function Cookie(delim){ //操作Cookie类 Array.prototype.splice=function(){ var len=arguments.length,tarray=[],i=arguments[0]+arguments[1]; if(len>1){ while(i< this.length)tarray[tarray.length]=this[i++]; this.length=arguments[0]; if(len>2)for(var i=2;i< len;i++)this[this.length]=arguments[i]; var tlen=tarray.length,i=0; while(i< tlen)this[this.length]=tarray[i++]; } return this; } this._Cookie=[]; this.Load=function(){ if(document.cookie.indexOf(";")!=-1){ var _sp,_name,_tp,_tars,_tarslength; var _item=document.cookie.split("; "); var _itemlength=_item.length; while(_itemlength>0){ _sp=_item[--_itemlength].split("="); _name=_sp[0]; _tp=_sp[1].split(","); _tars=_tp.slice(1,_tp.length); this._Cookie[_name]=[]; this._Cookie[_name]=_tars; this._Cookie[_name]["timeout"]=_tp[0]; } return true; } return false; } this.Save=function(){ var _str,_ars,_mars,_marslength,timeout,i,key; for(key in this._Cookie){ if(!this._Cookie[key])return; _str=[]; _mars=CookieClass._Cookie[key]; _marslength=_mars.length; for(i=0;i< _marslength;i++)_str[_str.length]=escape(_mars[i]); document.cookie=key+"="+_mars["timeout"]+(_str.length>0?",":"")+_str+";expires="+new Date(parseInt(_mars["timeout"])).toGMTString(); } } this.GetCookieCount=function(){ var _length=0,key; for(key in this._Cookie)_length++; return _length; } this.Create=function(name,days){ this._Cookie[name]=[]; this._Cookie[name]["timeout"]=new Date().getTime()+days*86400000; } this.Modify=function(name,days){ this.Create(name,days); } this.GetTime=function(name){ return new Date(parseInt(this._Cookie[name]["timeout"])); } this.Delete=function(name){ this.Create(name,0); } this.AddItem=function(name,value){ this._Cookie[name][this._Cookie[name].length]=value; } this.DelItem=function(name,index){ this._Cookie[name].splice(index,1); } this.GetCount=function(name){ return this._Cookie[name].length; } this.GetItem=function(name,index){ return this._Cookie[name][index]; } } < /script> < script type="text/javascript" language="javascript"> //返回cookie数量 var CookieClass=new Cookie(); if(!CookieClass.Load()){ CookieClass.Create("Pass",1); CookieClass.Create("ll",1); CookieClass.AddItem("Pass","Ps1"); CookieClass.AddItem("Pass","Ps2"); CookieClass.AddItem("Pass","Ps3"); CookieClass.AddItem("Pass","Ps4"); CookieClass.AddItem("Pass","Ps5"); CookieClass.AddItem("Pass","Ps6"); CookieClass.DelItem("Pass",1); CookieClass.Save(); } alert("Cookie过期时间:"+CookieClass.GetTime("Pass").toLocaleString()); alert(document.cookie); < /script> < p> Cookies对象的使用< br /> 以下是对象的公有方法:< br /> < br /> 方括号[]内是可选参数< br /> //构造< br /> Cookie([定界符,缺省为句点]) - 构造函数< br /> < br /> //初始化< br /> GetCookieCount() - 返回Cookies数量< br /> Create(name, days) - 创建Cookies及其时效天数< br /> Modify(name, days) - 修改Cookies的时效天数< br /> Delete(name) - 删除Cookies及其子项< br /> GetTime(name) - 返回指定Cookies的过期时间< br /> GetCount(name) - 返回Cookies的子项数量< br /> AddItem(name,value) - 增加一个子项< br /> GetItem(name,index) - 返回指定索引的子项< br /> DelItem(name,index) - 删除指定的子项< br /> < br /> //存取< br /> Load() - 读取Cookies< br /> Save() - 存储Cookies< /p> < /body> < /html>
以上就是ASP.NET中怎么对cookie进行操作,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
当前标题:ASP.NET中怎么对cookie进行操作
当前路径:http://ybzwz.com/article/giohsd.html