人亦已歌 发表于 2024-9-15 01:10:28

【C#、Asp.Net 工具类汇总】Cookie常见操作类

<p>使用实例:</p>
<pre>

private User u = new User();
private string urlPath =
protected void Page_Load(object sender, EventArgs e)
    if (!Page.IsPostBack)
    {
    #region 基础数据
    u = new User();
    u.UserName = "慈众营销";
    u.UserAge = 27;
    u.UserSex = "男";
    u.UserSite = "http://www.zhengdecai.com";
    urlPath = Server.MapPath("~/file/");
    #endregion
    cookieInfo();
    }
///&lt;summary&gt;
/// Cookie测试
///&lt;/summary&gt;
private void cookieInfo()
    CookieHelper.SetCookie("userName", "慈众营销");
    CookieHelper.SetCookie("userAge", "27", DateTime.Now.AddDays(1));
    CookieHelper.WriteCookie("userSex", "男");
    CookieHelper.WriteCookie("userSite", "http://www.zhengdecai.com", 20);
    //CookieHelper.SetCookie("u", u);
    CookieHelper.ClearCookie("userName");
    CookieHelper.RemoveCookie("userAge");
    string cStr = CookieHelper.GetCookieValue("userName") + " - " + CookieHelper.GetCookieValue("userAge") + " - " + CookieHelper.GetCookieValue("userSex") + " - " + CookieHelper.GetCookieValue("userSite");
    Response.Write("存在Cookie:" + cStr + "&lt;br/&gt;");
    CookieHelper.ClearCookie("userName");
    cStr = CookieHelper.GetCookieValue("userName") + " - " + CookieHelper.GetCookieValue("userAge") + " - " + CookieHelper.GetCookieValue("userSex") + " - " + CookieHelper.GetCookieValue("userSite");
    CookieHelper.ClearAllCookie();
    cStr = CookieHelper.GetCookieValue("userName") + " - " + CookieHelper.GetCookieValue("userAge") + " - " + CookieHelper.GetCookieValue("userSex") + " - " + CookieHelper.GetCookieValue("userSite");

</pre>

<p>类库信息:</p>
<pre>

///&lt;summary&gt;
/// Cookie 通用操作类
/// 1、ClearCookie(string strCookieName),清除指定Cookie对象
/// 2、ClearAllCookie(),清空所有的Cookie对象
/// 3、RemoveCookie(string strCookieName),删除指定Session对象
/// 4、GetCookieValue(string strCookieName),获取指定Cookie值
/// 5、SetCookie(string strCookieName, string strCookieValue),设置一个Cookie(24小时过期)
/// 6、SetCookie(string strCookieName, string strCookieValue, DateTime iExpires),设置一个Cookie,指定过期时间iExpires
/// 7、WriteCookie(string strCookieName, string strCookieValue),设置Cookie值
/// 8、WriteCookie(string strCookieName, string strCookieValue, int iExpires),设置Cookie值,设置过期时间iExpires分钟
///&lt;/summary&gt;
public class CookieHelper
    #region 清除Cookie信息
    ///&lt;summary&gt;
    /// 清除指定Cookie对象
    ///&lt;/summary&gt;
    ///&lt;param name="strCookieName"&gt;Cookie对象名&lt;/param&gt;
    public static void ClearCookie(string strCookieName)
      HttpCookie Cookie = HttpContext.Current.Request.Cookies;
      if (Cookie != null)
      {
            Cookie.Expires = DateTime.Now.AddYears(-3);
            HttpContext.Current.Response.Cookies.Add(Cookie);
      }
    }
    ///&lt;summary&gt;
    /// 清空所有的Cookie对象
    ///&lt;/summary&gt;
    public static void ClearAllCookie()
      HttpContext.Current.Request.Cookies.Clear();
    }
    ///&lt;summary&gt;
    /// 删除指定Session对象
    ///&lt;/summary&gt;
    public static void RemoveCookie(string strCookieName)
      HttpContext.Current.Request.Cookies.Remove(strCookieName);
    }
    #endregion
    #region 获取、设置Cookie信息
    ///&lt;summary&gt;
    /// 获取指定Cookie值
    ///&lt;/summary&gt;
    ///&lt;param name="strCookieName"&gt;strCookieName&lt;/param&gt;
    public static string GetCookieValue(string strCookieName)
      HttpCookie Cookie = HttpContext.Current.Request.Cookies;
      string str = string.Empty;
      if (Cookie != null)
      {
            str = Cookie.Value;
      }
      return str;
    }
    ///&lt;summary&gt;
    /// 设置一个Cookie(24小时过期)
    ///&lt;/summary&gt;
    ///&lt;param name="strCookieName"&gt;&lt;/param&gt;
    ///&lt;param name="strCookieValue"&gt;&lt;/param&gt;
    public static void SetCookie(string strCookieName, string strCookieValue)
      SetCookie(strCookieName, strCookieValue, DateTime.Now.AddDays(1.0));
    }
    ///&lt;summary&gt;
    /// 设置一个Cookie,指定过期时间iExpires
    ///&lt;/summary&gt;
    ///&lt;param name="strCookieName"&gt;Cookie名&lt;/param&gt;
    ///&lt;param name="strCookieValue"&gt;Cookie值&lt;/param&gt;
    ///&lt;param name="iExpires"&gt;过期时间 DateTime&lt;/param&gt;
    public static void SetCookie(string strCookieName, string strCookieValue, DateTime iExpires)
      HttpCookie Cookie = new HttpCookie(strCookieName)
      {
            Value = strCookieValue,
            Expires = iExpires
      };
      HttpContext.Current.Response.Cookies.Add(Cookie);
    }
    #endregion
    #region 设置HTTP Cookie信息
    ///&lt;summary&gt;
    /// 设置Cookie值
    ///&lt;/summary&gt;
    ///&lt;param name="strCookieName"&gt;名称&lt;/param&gt;
    ///&lt;param name="strCookieValue"&gt;值&lt;/param&gt;
    public static void WriteCookie(string strCookieName, string strCookieValue)
      HttpCookie Cookie = HttpContext.Current.Request.Cookies;
      if (Cookie == null)
      {
            Cookie = new HttpCookie(strCookieName);
      }
      Cookie.Value = strCookieValue;
      HttpContext.Current.Response.AppendCookie(Cookie);
    }
    ///&lt;summary&gt;
    /// 设置Cookie值,设置过期时间iExpires分钟
    ///&lt;/summary&gt;
    ///&lt;param name="strCookieName"&gt;名称&lt;/param&gt;
    ///&lt;param name="strCookieValue"&gt;值&lt;/param&gt;
    ///&lt;param name="iExpires"&gt;过期时间(分钟)&lt;/param&gt;
    public static void WriteCookie(string strCookieName, string strCookieValue, int iExpires)
      HttpCookie Cookie = HttpContext.Current.Request.Cookies;
      if (Cookie == null)
      {
            Cookie = new HttpCookie(strCookieName);
      }
      Cookie.Value = strCookieValue;
      Cookie.Expires = DateTime.Now.AddMinutes(iExpires);
      HttpContext.Current.Response.AppendCookie(Cookie);
    }
    #endregion

</pre>

<p>以上类库内容来源互联网,站长稍作整理</p>
页: [1]
查看完整版本: 【C#、Asp.Net 工具类汇总】Cookie常见操作类