C# Cookie操作

來源:互聯網
上載者:User
 

操作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)         {    #region用Cookie進行儲存登入用戶名              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}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.