天天看點

操作Cookie公用代碼

操作Cookie公用代碼

#region關于操作Cookie的方法

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

         /**////<summary>

操作Cookie公用代碼
操作Cookie公用代碼

         ///建立cookie值

操作Cookie公用代碼
操作Cookie公用代碼

         ///</summary>

操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

{

操作Cookie公用代碼
操作Cookie公用代碼

              HttpCookie cookie = new HttpCookie(cookieName);

操作Cookie公用代碼
操作Cookie公用代碼

              cookie.Value=cookieValue;

操作Cookie公用代碼
操作Cookie公用代碼

              //DateTime dtNow = DateTime.Now ;

操作Cookie公用代碼
操作Cookie公用代碼

              //TimeSpan tsMinute = cookieTime;

操作Cookie公用代碼
操作Cookie公用代碼

              cookie.Expires = cookieTime;

操作Cookie公用代碼
操作Cookie公用代碼

              Response.Cookies.Add(cookie);

操作Cookie公用代碼
操作Cookie公用代碼

         }

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

         ///<param name="subCookieName">子資訊cookie名稱</param>

操作Cookie公用代碼
操作Cookie公用代碼

         ///<param name="subCookieValue">子資訊cookie值</param>

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

              cookie[subCookieName]=subCookieValue;

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

         ///取得cookie的值

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

         ///<returns></returns>

操作Cookie公用代碼
操作Cookie公用代碼

         private string GetCookieValue(string cookieName)

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

              string cookieValue="";

操作Cookie公用代碼
操作Cookie公用代碼

              HttpCookie cookie = Request.Cookies[cookieName];             

操作Cookie公用代碼
操作Cookie公用代碼

              if(null == cookie)

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

                   cookieValue="";

操作Cookie公用代碼
操作Cookie公用代碼

              }

操作Cookie公用代碼
操作Cookie公用代碼

              else

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

                   cookieValue=cookie.Value;

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

              return cookieValue;

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

         ///<param name="subCookieName">cookie子資訊值</param>

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

         public static string GetCookieValue(string cookieName, string subCookieName)

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

            string cookieValue = "";

操作Cookie公用代碼

            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];

操作Cookie公用代碼

            if (null == cookie)

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

                cookieValue = "";

操作Cookie公用代碼

            }

操作Cookie公用代碼

            else

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

                cookieValue = cookie.Value;

操作Cookie公用代碼

                string[] words = cookieValue.Split('&');

操作Cookie公用代碼

                foreach (string s in words)

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

                    if (s.IndexOf(subCookieName+"=") >= 0)

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

                        cookieValue = s.Split('=')[1];

操作Cookie公用代碼

                    }

操作Cookie公用代碼
操作Cookie公用代碼

                }

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼

            return cookieValue;

操作Cookie公用代碼

        }

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼

         private void RemoteCookieValue(string cookieName)

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

         #endregion

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

以下是調用:

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

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

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

{    

操作Cookie公用代碼
操作Cookie公用代碼

#region用Cookie進行儲存登入使用者名

操作Cookie公用代碼
操作Cookie公用代碼

              if(this.chb_IsSave.Checked)

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

                   //将使用者儲存一個小時,具體設定可以進行調整。。

操作Cookie公用代碼
操作Cookie公用代碼

                   //這裡用了固定的公用的cookie使用者UserName,使用者編号UserID進行通路

操作Cookie公用代碼
操作Cookie公用代碼

                   CreateCookieValue("UserName","UserName","UserID",this.txt_UserID.Text,DateTime.Now+new TimeSpan(0,1,0,0));//設定儲存使用者名

操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼
操作Cookie公用代碼

              #endregion

操作Cookie公用代碼
操作Cookie公用代碼

}

操作Cookie公用代碼
操作Cookie公用代碼