01
2015
06

【C#、Asp.Net 工具类大全】Cookie常用操作类

使用实例:

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();
    }
}
/// <summary>
/// Cookie测试
/// </summary>
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 + "<br />");
    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");
}

类库信息:

/// <summary>
/// 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分钟
/// </summary>
public class CookieHelper
{
    #region 清除Cookie信息
    /// <summary>
    /// 清除指定Cookie对象
    /// </summary>
    /// <param name="strCookieName">Cookie对象名</param>
    public static void ClearCookie(string strCookieName)
    {
        HttpCookie Cookie = HttpContext.Current.Request.Cookies[strCookieName];
        if (Cookie != null)
        {
            Cookie.Expires = DateTime.Now.AddYears(-3);
            HttpContext.Current.Response.Cookies.Add(Cookie);
        }
    }
    /// <summary>
    /// 清空所有的Cookie对象
    /// </summary>
    /// <param name="strCookieName">Cookie对象名</param>
    public static void ClearAllCookie()
    {
        HttpContext.Current.Request.Cookies.Clear();
    }
    /// <summary>
    /// 删除指定Session对象
    /// </summary>
    /// <returns></returns>
    public static void RemoveCookie(string strCookieName)
    {
        HttpContext.Current.Request.Cookies.Remove(strCookieName);
    }
    #endregion
    #region 获取、设置Cookie信息
    /// <summary>
    /// 获取指定Cookie值
    /// </summary>
    /// <param name="strCookieName">strCookieName</param>
    /// <returns></returns>
    public static string GetCookieValue(string strCookieName)
    {
        HttpCookie Cookie = HttpContext.Current.Request.Cookies[strCookieName];
        string str = string.Empty;
        if (Cookie != null)
        {
            str = Cookie.Value;
        }
        return str;
    }
    /// <summary>
    /// 设置一个Cookie(24小时过期)
    /// </summary>
    /// <param name="strCookieName"></param>
    /// <param name="strCookieValue"></param>
    public static void SetCookie(string strCookieName, string strCookieValue)
    {
        SetCookie(strCookieName, strCookieValue, DateTime.Now.AddDays(1.0));
    }
    /// <summary>
    /// 设置一个Cookie,指定过期时间iExpires
    /// </summary>
    /// <param name="strCookieName">Cookie名</param>
    /// <param name="strCookieValue">Cookie值</param>
    /// <param name="iExpires">过期时间 DateTime</param>
    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信息
    /// <summary>
    /// 设置Cookie值
    /// </summary>
    /// <param name="strCookieName">名称</param>
    /// <param name="strCookieValue">值</param>
    public static void WriteCookie(string strCookieName, string strCookieValue)
    {
        HttpCookie Cookie = HttpContext.Current.Request.Cookies[strCookieName];
        if (Cookie == null)
        {
            Cookie = new HttpCookie(strCookieName);
        }
        Cookie.Value = strCookieValue;
        HttpContext.Current.Response.AppendCookie(Cookie);
    }
    /// <summary>
    /// 设置Cookie值,设置过期时间iExpires分钟
    /// </summary>
    /// <param name="strCookieName">名称</param>
    /// <param name="strCookieValue">值</param>
    /// <param name="iExpires">过期时间(分钟)</param>
    public static void WriteCookie(string strCookieName, string strCookieValue, int iExpires)
    {
        HttpCookie Cookie = HttpContext.Current.Request.Cookies[strCookieName];
        if (Cookie == null)
        {
            Cookie = new HttpCookie(strCookieName);
        }
        Cookie.Value = strCookieValue;
        Cookie.Expires = DateTime.Now.AddMinutes(iExpires);
        HttpContext.Current.Response.AppendCookie(Cookie);
    }
    #endregion
}

以上类库内容来源互联网,站长稍作整理

« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。