C#执行对cookies的增删查操作

公司有一些WEB项目使用的是cookies验证,最近对这些项目进行了部分修改,整理了一套cookie的增删改查代码,记录一下具体代码内容,方便以后快速查找,时刻为偷懒做准备

/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
/// <param name="strValue">过期时间(分钟)</param>
public static void WriteCookie(string strName, string strValue, int expires)
{
    HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
    if (cookie == null)
    {
        cookie = new HttpCookie(strName);
    }
    cookie.Value = UrlEncode(strValue);
    cookie.Expires = DateTime.Now.AddMinutes(expires);
    HttpContext.Current.Response.AppendCookie(cookie);
}
  
  
/// <summary>
/// 取得CookieValue
/// </summary>
/// <param name="CookieName">Cookie名称</param>
/// <returns>Cookie的值</returns>
public static 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 static void RemoveCookie(string cookieName)
{
    HttpCookie myCookie = new HttpCookie(cookieName);
    DateTime now = DateTime.Now;
    myCookie.Expires = now.AddYears(-2);
    HttpContext.Current.Response.Cookies.Add(myCookie);
}
THE END