国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

asp.net操作cookie

2019-11-17 01:50:02
字體:
來源:轉載
供稿:網友

asp.net操作cookie

一、添加cookie

C# 代碼 復制
//方式1:Response.Cookies["username"].value="gggg";Response.Cookies["username"].Expires=DateTime.MaxValue; //方式2:HttpCookie acookie = new HttpCookie("last");acookie.Value="a";acookie..Expires=DateTime.MaxValue; Response.Cookies.Add(acookie);//多值Cookie的寫法 //方式1:Response.Cookies["userinfo1"]["name"].value="aaa";Response.Cookies["userinfo1"]["last"].value="a";Response.Cookies["userinfo1"].Expires=DateTime.MaxValue; //方式2:HttpCookie cookie = new HttpCookie("userinfo1");cookie.Values["name"]="aaa";cookie.Values["last"]="a";cookie.Expires=DateTime.MaxValue; //cookie.Expires = System.DateTime.Now.AddDays(1);//設置過期時間  1天Response.Cookies.Add(cookie);

二、讀取Cookie

Internet Explorer 將站點的 Cookie 保存在文件名格式為 <user>@<domain>.txt 的文件中,其中 <user> 是您的帳戶名。

C# 代碼 復制
if (Request.Cookies["userName"]!=null){  string str = Request.Cookies("userName").Value; }//多值Cookie的讀取if (Request.Cookies["userInfo1"]!=null ){  string name=Request.Cookies["userInfo1"]["name"];  string last=Request.Cookies["userInfo1"]["last"]; }//讀取 Cookie 集合for(int i = 0 ;i<Request.Cookies.Count ;i++){    HttpCookie cookies = Request.Cookies;    Response.Write("name="+cookies.Mame+"<br/>");    if (cookies.HasKeys )//是否有子鍵    {        System.Collections.Specialized.NameValueCollection NameColl                                              = aCookie.Values ;        for(int j=0;j<NameColl.Count;j++)        {            Response.Write("子鍵名="+ NameColl.AllKey[j] +"<br/>");            Response.Write("子鍵值="+ NameColl[j] +"<br/>");        }    }    else    {        Response.Write("value="+cookies.Value+"<br/>");            }}

注意:在獲取Cookie的值之前,應該確保該 Cookie 確實存在。否則,您將得到一個異常

三、修改 Cookie

修改的方法與創建方法相同

四、刪除 Cookie

將其有效期設置為過去的某個日期。當瀏覽器檢查 Cookie 的有效期時,就會刪除這個已過期的 Cookie。

C# 代碼 復制
//刪除cookie下的屬性 HttpCookie acookie=Request.Cookies["Info"]; acookie.Values.Remove("userid"); acookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.A
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肃北| 喜德县| 延津县| 古交市| 温泉县| 大名县| 永嘉县| 顺平县| 永靖县| 屯留县| 德化县| 海南省| 天柱县| 阳江市| 黄梅县| 嘉兴市| 海口市| 石楼县| 天全县| 哈密市| 许昌县| 盈江县| 和顺县| 东乌珠穆沁旗| 河西区| 安图县| 永兴县| 万宁市| 鹤壁市| 固镇县| 巴中市| 新密市| 岳西县| 道孚县| 平湖市| 沙坪坝区| 肇东市| 铁力市| 赤水市| 沈丘县| 景德镇市|