C#操作cookie的例子(读取、删除、写入)

发布时间:2019-09-17编辑:脚本学堂
本文介绍下,C#语言操作cookie的例子,包括读取cookie、写入cookie、删除cookie的操作。有需要的朋友参考下吧。

代码如下,具体实现看注释。
 

复制代码 代码示例:

#region Cookie处理 
/// <summary> 
/// 写cookie值 
/// </summary> 
/// <param name="strName">名称</param> 
/// <param name="strValue">值</param> 
public static void WriteCookie(string strName, string strValue) 

      HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; 
      if (cookie == null) 
      { 
          cookie = new HttpCookie(strName); 
      } 
      cookie.Value = strValue; 
      HttpContext.Current.Response.AppendCookie(cookie); 
     
  } 
     
/// <summary> 
/// 写cookie值 
/// </summary>  www.jb200.com
/// <param name="strName">名称</param> 
/// <param name="key">索引</param> 
/// <param name="strValue">值</param> 
public static void WriteCookie(string strName, string key, string strValue) 

      HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; 
      if (cookie == null) 
      { 
          cookie = new HttpCookie(strName); 
      } 
      cookie[key] = strValue; 
      HttpContext.Current.Response.AppendCookie(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 = strValue; 
      cookie.Expires = DateTime.Now.AddMinutes(expires); 
      HttpContext.Current.Response.AppendCookie(cookie); 
     
  } 
     
/// <summary> 
/// 写cookie值 
/// </summary> 
/// <param name="strName">名称</param> 
/// <param name="strValue">值</param> 
/// <param name="strValue">过期时间(分钟)</param> 
/// <param name="strValue">cookie存在的域范围</param> 
public static void WriteCookie(string strName, string strValue, int expires, string domain) 

      HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; 
      if (cookie == null) 
      { 
          cookie = new HttpCookie(strName); 
      } 
      cookie.Value = strValue; 
      cookie.Expires = DateTime.Now.AddMinutes(expires); 
      cookie.Domain = domain; 
     
      HttpContext.Current.Response.AppendCookie(cookie); 
      
       
  } 
     
  /// <summary> 
  /// 读cookie值 
  /// </summary> 
  /// <param name="strName">名称</param> 
  /// <returns>cookie值</returns> 
  public static string GetCookie(string strName) 
  { 
      if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null) 
      { 
          return HttpContext.Current.Request.Cookies[strName].Value.ToString(); 
      } 
     
      return ""; 
  } 
  /// <summary> 
  /// 读cookie值 
  /// </summary> 
  /// <param name="strName">名称</param> 
  /// <returns>cookie值</returns> 
  public static string GetCookie(string strName, string key) 
  { 
      if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null) 
      { 
          return HttpContext.Current.Request.Cookies[strName][key].ToString(); 
      } 
     
      return ""; 
  } 
     
  /// <summary> 
  /// 删除Cookies 
  /// </summary> 
  /// <param name="strName">名称</param> 
  /// <param name="domain">域名</param> 
  /// <returns></returns> 
  public static void DelCookie(string strName, string domain) 
  { 
      HttpCookie cookie = new HttpCookie(strName); 
      WriteCookie(strName, "", -60, domain); 
  } 
     
  /// <summary> 
  /// 删除Cookies 
  /// </summary> 
  /// <param name="strName">名称</param> 
  /// <returns></returns> 
  public static void DelCookie(string strName) 
  { 
      HttpCookie cookie = new HttpCookie(strName); 
      WriteCookie(strName, "", -60); 
  } 
  #endregion