关于Asp.net中Cookie的使用-收集2(写入、读取、清除Cookie的类 )

    技术2022-05-11  6

    写入、读取、清除Cookie的类

      /// <summary>  /// 保存一个Cookie  /// </summary>  /// <param name="CookieName">Cookie名称</param>  /// <param name="CookieValue">Cookie值</param>  /// <param name="CookieTime">Cookie过期时间(小时),0为关闭页面失效</param>  public void SaveCookie(string CookieName,string CookieValue,double CookieTime)  {   HttpCookie myCookie = new HttpCookie(CookieName);   DateTime now = DateTime.Now;

       myCookie.Value = CookieValue;

       if(CookieTime!=0)   {    myCookie.Expires = now.AddDays(CookieTime);    if(HttpContext.Current.Response.Cookies[CookieName]!=null)     HttpContext.Current.Response.Cookies.Remove(CookieName);

        HttpContext.Current.Response.Cookies.Add(myCookie);   }   else   {    if(HttpContext.Current.Response.Cookies[CookieName]!=null)     HttpContext.Current.Response.Cookies.Remove(CookieName);

        HttpContext.Current.Response.Cookies.Add(myCookie);   }  }

      /// <summary>  /// 取得CookieValue  /// </summary>  /// <param name="CookieName">Cookie名称</param>  /// <returns>Cookie的值</returns>  public string GetCookie(string CookieName)  {   HttpCookie myCookie = new HttpCookie(CookieName);   myCookie = HttpContext.Current.Request.Cookies[CookieName];

       if (myCookie != null)    return myCookie.Value;   else    return null;  }

      /// <summary>  /// 清除CookieValue  /// </summary>  /// <param name="CookieName">Cookie名称</param>  public void ClearCookie(string CookieName)  {   HttpCookie myCookie = new HttpCookie(CookieName);   DateTime now = DateTime.Now;

       myCookie.Expires = now.AddYears(-2);

       HttpContext.Current.Response.Cookies.Add(myCookie);  }

    Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=378329 


    最新回复(0)