您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關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進行操作,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。