cookie使用汇总 c设置ookie的生命周期

protected void btnSearch_Click(object sender, EventArgs e)
        
{
            //生成条件表达式
            string where = bll.GetWhereSql(Int32.Parse(ddlCate.SelectedValue),
                        Int32.Parse(ddlHarbor.SelectedValue), 00, ddlState.SelectedValue, tbKeyword.Text.Trim());
            //判断条件Cookie是否为空,如不为空,则移除现有条件Cookie
            if (Request.Cookies["Admin_HarborSpot_Where"] != null)
            {
                RemoveWhereCookie(where);//移除现有条件Cookie
            }

            //设置一个新的条件Cookie
            SetWhereCookie(where);
            
            BindData(true);
        }


//设置条件Cookie
        private void SetWhereCookie(string where)
        
{
            HttpCookie cookie = new HttpCookie("Admin_HarborSpot_Where");
            cookie.Value = where;
            cookie.Expires = DateTime.Now.AddDays(1d);
            Response.Cookies.Add(cookie);
        }


        
//移除现存的条件Cookie
        private void RemoveWhereCookie(string where)
        
{
            HttpCookie cookie = Request.Cookies["Admin_HarborSpot_Where"];
            cookie.Value = where;
            cookie.Expires = DateTime.Now.AddDays(-3d);
            Response.Cookies.Add(cookie);
        }

原文地址:https://www.cnblogs.com/zqn518/p/2380380.html