c#.net在Web页中设置Cookies

一、设置cookies的方法很简单,有以下两种方法:  

1、直接添加Cookie值:  
   Response.Cookies["userName"] = "Tom";   
   Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1) ; \\过期时间,在Cookies文件中无法查看,也不能调用.  

2、创建Cookie对象的一个实例:  
   HttpCookie cookie=new HttpCookie("userName");  
   cookie.Value = "Tom";  
   cookie.Expires = DateTime.Now.AddDays(1) ;   
   Response.Cookies.Add(aCookie)   

用以上任一方法都可以生成一个有“userName”项的文件, 在你的Internet临时文件夹中你可以查看它。  

也可以创建和添加有子键的Cookies,如:  
Response.Cookies["userInfo"]["userName"] = "Tom";   

或:  
   HttpCookie cookie=new HttpCookie("userInfo");  
   cookie.Values["userName"] = "Tom";  
   aCookie.Expires = DateTime.Now.AddDays(1);   
   Response.Cookies.Add(aCookie)   

二、检索Cookies:  
  Cookies某一键的值为:   
  Server.HtmlEncode(Request.Cookies["userInfo"]["userName"])  
  你可以用Response.Write()方法输出它到页面,如:  
  Response.Write(Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]))  

  或赋值给其它变量:  

  string strCookie1=Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]);  
    
  Cookies[i]数组可以检索所有项和子键,如:  
  string[] cooName  = new string[Request.Cookies.Count];  
  string[] cooValue = new string[Request.Cookies.Count];    
  HttpCookie aCookie;  
  for(int i=0;i<Request.Cookies.Count;i++){  
    aCookie = Request.Cookies[i];   
    cooName[i]  = Server.HtmlEncode(aCookie.Name);  
    if(!aCookie.HasKeys){  
       cooValue[i] = Server.HtmlEncode(aCookie.Value);  
    }else{  
      string[] subcooName  = new string[aCookie.Values.Count];  
      string[] subcooValue = new string[aCookie.Values.Count];   
      for(int j=0;j<aCookie.Values.Count;j++){    
        subcooName[j]  = Server.HtmlEncode(aCookie.Values.AllKeys[j]);  
        subcooValue[j] = Server.HtmlEncode(aCookie.Values[j]);  
      }  
    }  
  }  

三、修改Cookies  
   如果是数值类型的Cookie值,比如访问次数,你可以读取该值进行加减操作后再存回,一般的修改直接存入新值就可以了,系统自动用新值覆盖原值,存入的方法与创建相同。  

四、删除Cookies  
删除Cookies只要把有效期设为失效就可以了,如在创建时设有效期为一天:  
  cookie.Expires = DateTime.Now.AddDays(1) ;  
要删除则设为:  
cookie.Expires = DateTime.Now.AddDays(-1) ;  

  删除子键:  
HttpCookie cookie;  
cookie = Request.Cookies["userInfo"];  
  aCookie.Values.Remove("userName");  
  aCookie.Expires = DateTime.Now.AddDays(1);  
  Response.Cookies.Add(aCookie);  


操作Cookie公用代碼

         

#region關於操作Cookie的方法

         ///<summary>

         ///創建cookie

         ///</summary>

         ///<param name="cookieName">cookie名稱</param>

         ///<param name="cookieValue">cookie</param>

         ///<param name="cookieTime">cookie有效時間</param>

         private void CreateCookieValue(string cookieName,string cookieValue,DateTime cookieTime)

         {

              HttpCookie cookie = new HttpCookie(cookieName);

              cookie.Value=cookieValue;

              //DateTime dtNow = DateTime.Now ;

              //TimeSpan tsMinute = cookieTime;

              cookie.Expires = cookieTime;

              Response.Cookies.Add(cookie);

         }

         ///<summary>

         ///創建cookie

         ///</summary>

         ///<param name="cookieName">cookie名稱</param>    

         ///<param name="cookieValue">cookie</param>

         ///<param name="subCookieName">子信息cookie名稱</param>

         ///<param name="subCookieValue">子信息cookie</param>

         ///<param name="cookieTime">cookie有效時間</param>

         private void CreateCookieValue(string cookieName,string cookieValue,string subCookieName,string subCookieValue,DateTime cookieTime)

         {

              HttpCookie cookie = new HttpCookie(cookieName);

              cookie.Value=cookieValue;

              cookie[subCookieName]=subCookieValue;

              cookie.Expires = cookieTime;

              Response.Cookies.Add(cookie);

         }

         ///<summary>

         ///取得cookie的值

         ///</summary>

         ///<param name="cookieName">cookie名稱</param>

         ///<returns></returns>

         private string GetCookieValue(string cookieName)

         {

              string cookieValue="";

              HttpCookie cookie = Request.Cookies[cookieName];             

              if(null == cookie)

              {

                   cookieValue="";

              }

              else

              {

                   cookieValue=cookie.Value;

              }

              return cookieValue;

         }

         ///<summary>

         ///取得cookie的值

         ///</summary>

         ///<param name="cookieName">cookie名稱</param>

         ///<param name="subCookieName">cookie子信息值</param>

         ///<returns></returns>

         private string GetCookieValue(string cookieName,string subCookieName)

         {

              string cookieValue="";

              HttpCookie cookie = Request.Cookies[cookieName];             

              if(null == cookie)

              {

                   cookieValue="";

              }

              else

              {

                   cookieValue=cookie.Value;

                   cookieValue=cookieValue.Split('&')[1].ToString().Split('=')[1];

              }

              return cookieValue;

         }

         ///<summary>

         ///刪除某個固定的cookie[此方法一是在原有的cookie上再創建同樣的cookie值,但是時間是過期的時間]

         ///</summary>

         ///<param name="cookieName"></param>

         private void RemoteCookieValue(string cookieName)

         {

              string dt="1900-01-01 12:00:00";

              CreateCookieValue(cookieName,"",Convert.ToDateTime(dt));

         }

         #endregion

 

以下是調用:

 

         private void Page_Load(object sender, System.EventArgs e)

         {

              // 在這裡放置使用者程式碼以初始化網頁     

              this.txt_UserID.Text=this.GetCookieValue("UserName","UserID");//取得用戶名

         }

 

         private void btn_Submit_Click(object sender, System.EventArgs e)

         {    

#regionCookie進行保存登入用戶名

              if(this.chb_IsSave.Checked)

              {

                   //將用戶保存一個小時,具體設置可以進行調整。。

                   //這裡用了固定的公用的cookie用戶UserName,用戶編號UserID進行訪問

                   CreateCookieValue("UserName","UserName","UserID",this.txt_UserID.Text,DateTime.Now+new TimeSpan(0,1,0,0));//設置保存用戶名

              }

              #endregion

}

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Hopewell_Go/archive/2007/03/02/1518799.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值