C#中Cookies的存取

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   for   

<1>

C#中Cookies的存取 [csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace WebApplication1  
  7. {  
  8.     public class FunCookie  
  9.     {  
  10.         /// <summary>  
  11.         /// 建立Cookie和賦值,及設計Cookie有效天數  
  12.         /// </summary>  
  13.         /// <param name="strCookieName">Cookie名字</param>  
  14.         /// <param name="strCookieValue">Cookie的值</param>  
  15.         /// <param name="intDay">Cookie有效天數</param>  
  16.         /// <returns>布爾值</returns>  
  17.         public static bool SetCookie(string strCookieName,string strCookieValue,int intDay )  
  18.         {  
  19.             try  
  20.             {  
  21.                 HttpCookie cookie = new HttpCookie(strCookieName); //建立一個cookie對象  
  22.                 cookie.Value = strCookieValue; //設定cookie的值  
  23.                 cookie.Expires = DateTime.Now.AddDays(intDay); //或cookie.Expires.AddDays(intDay);設定cookie的有效期間  
  24.                 System.Web.HttpContext.Current.Response.Cookies.Add(cookie); //將cookie添加到cookies中  
  25.                 return true;  
  26.             }  
  27.             catch  
  28.             {  
  29.                 return false;  
  30.             }  
  31.         }  
  32.   
  33.         /// <summary>  
  34.         /// 根據Cookie的名字擷取Cookie的值  
  35.         /// </summary>  
  36.         /// <param name="strCookieName">要擷取的Cookie的名字</param>  
  37.         /// <returns>Cookie的值(string類型)</returns>  
  38.         public static string GetCookie(string strCookieName)  
  39.         {  
  40.             HttpCookie cookie= HttpContext.Current.Request.Cookies[strCookieName];//擷取cookie  
  41.             if (cookie != null)  
  42.             {  
  43.                 return cookie.Value; //返回cookie的值  
  44.             }  
  45.             else  
  46.             {  
  47.                 return null;  
  48.             }  
  49.         }  
  50.   
  51.         /// <summary>  
  52.         /// 刪除Cookie  
  53.         /// </summary>  
  54.         /// <param name="strCookieName"></param>  
  55.         /// <returns></returns>  
  56.         public static bool DeleteCookie(string strCookieName)  
  57.         {  
  58.             try  
  59.             {  
  60.                 HttpCookie cookie = new HttpCookie(strCookieName);  
  61.                 cookie.Expires = DateTime.Now.AddDays(-1);   
  62.                 HttpContext.Current.Response.Cookies.Add(cookie);  
  63.                 return true;  
  64.             }  
  65.             catch  
  66.             {  
  67.                 return false;  
  68.             }  
  69.         }  
  70.   
  71.           
  72.     }  

C#中Cookies的存取

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.