asp.net操作cookie

一、添加cookie 

 

C# 代码    复制
asp.net操作cookie asp.net操作cookie//方式1: asp.net操作cookieResponse.Cookies["username"].value="gggg"; asp.net操作cookieResponse.Cookies["username"].Expires=DateTime.MaxValue; asp.net操作cookie asp.net操作cookie//方式2: asp.net操作cookieHttpCookie acookie = new HttpCookie("last"); asp.net操作cookieacookie.Value="a"; asp.net操作cookieacookie..Expires=DateTime.MaxValue; asp.net操作cookieResponse.Cookies.Add(acookie); asp.net操作cookie asp.net操作cookie//多值Cookie的写法 asp.net操作cookie asp.net操作cookie//方式1: asp.net操作cookieResponse.Cookies["userinfo1"]["name"].value="aaa"; asp.net操作cookieResponse.Cookies["userinfo1"]["last"].value="a"; asp.net操作cookieResponse.Cookies["userinfo1"].Expires=DateTime.MaxValue; asp.net操作cookie asp.net操作cookie//方式2: asp.net操作cookieHttpCookie cookie = new HttpCookie("userinfo1"); asp.net操作cookiecookie.Values["name"]="aaa"; asp.net操作cookiecookie.Values["last"]="a"; asp.net操作cookiecookie.Expires=DateTime.MaxValue; asp.net操作cookie//cookie.Expires = System.DateTime.Now.AddDays(1);//设置过期时间 1天 asp.net操作cookieResponse.Cookies.Add(cookie); asp.net操作cookie

 

二、读取Cookie 

Internet Explorer 将站点的 Cookie 保存在文件名格式为 <user>@<domain>.txt 的文件中,其中 <user> 是您的帐户名。

 

C# 代码    复制
asp.net操作cookie asp.net操作cookieif (Request.Cookies["userName"]!=null) asp.net操作cookie{ asp.net操作cookie string str = Request.Cookies("userName").Value; asp.net操作cookie} asp.net操作cookie asp.net操作cookie//多值Cookie的读取 asp.net操作cookieif (Request.Cookies["userInfo1"]!=null ) asp.net操作cookie{ asp.net操作cookie string name=Request.Cookies["userInfo1"]["name"]; asp.net操作cookie string last=Request.Cookies["userInfo1"]["last"]; asp.net操作cookie} asp.net操作cookie asp.net操作cookie asp.net操作cookie//读取 Cookie 集合 asp.net操作cookiefor(int i = 0 ;i<Request.Cookies.Count ;i++) asp.net操作cookie{ asp.net操作cookie HttpCookie cookies = Request.Cookies; asp.net操作cookie Response.Write("name="+cookies.Mame+"<br/>"); asp.net操作cookie if (cookies.HasKeys )//是否有子键 asp.net操作cookie { asp.net操作cookie System.Collections.Specialized.NameValueCollection NameColl asp.net操作cookie = aCookie.Values ; asp.net操作cookie for(int j=0;j<NameColl.Count;j++) asp.net操作cookie { asp.net操作cookie Response.Write("子键名="+ NameColl.AllKey[j] +"<br/>"); asp.net操作cookie Response.Write("子键值="+ NameColl[j] +"<br/>"); asp.net操作cookie } asp.net操作cookie asp.net操作cookie } asp.net操作cookie else asp.net操作cookie { asp.net操作cookie Response.Write("value="+cookies.Value+"<br/>"); asp.net操作cookie } asp.net操作cookie} asp.net操作cookie


注意:在获取Cookie的值之前,应该确保该 Cookie 确实存在。否则,您将得到一个异常

 

三、修改 Cookie 


修改的方法与创建方法相同

 

四、删除 Cookie 


将其有效期设置为过去的某个日期。当浏览器检查 Cookie 的有效期时,就会删除这个已过期的 Cookie。
 

 
C# 代码    复制
asp.net操作cookie asp.net操作cookie//删除cookie下的属性 asp.net操作cookie asp.net操作cookie asp.net操作cookie HttpCookie acookie=Request.Cookies["Info"]; asp.net操作cookie acookie.Values.Remove("userid"); asp.net操作cookie acookie.Expires = DateTime.Now.AddDays(1); asp.net操作cookie Response.Cookies.Add(acookie); asp.net操作cookie asp.net操作cookie//删除所有cookie,就是设置过期时间为现在就行了 asp.net操作cookie asp.net操作cookie asp.net操作cookie int limit=Request.Cookies.Count - 1; asp.net操作cookie for(int i=0;i<limit;i++) asp.net操作cookie { asp.net操作cookie acookie = Request.Cookies(i) asp.net操作cookie acookie.Expires = DateTime.Now.AddDays(-1) asp.net操作cookie Response.Cookies.Add(acookie) asp.net操作cookie } asp.net操作cookie

 

五、ASP.NET对Cookie操作的公用类

 

 
C# 代码    复制
asp.net操作cookie asp.net操作cookie asp.net操作cookieusing System; asp.net操作cookieusing System.Collections.Generic; asp.net操作cookieusing System.Linq; asp.net操作cookieusing System.Text; asp.net操作cookieusing System.Web; asp.net操作cookieusing System.Collections.Specialized; asp.net操作cookie asp.net操作cookie asp.net操作cookienamespace Core.Common.Web asp.net操作cookie{ asp.net操作cookie asp.net操作cookie /// <summary> asp.net操作cookie /// Cookie静态操作类 asp.net操作cookie /// </summary> asp.net操作cookie asp.net操作cookie public static class Cookie asp.net操作cookie { asp.net操作cookie asp.net操作cookie #region 静态方法 asp.net操作cookie asp.net操作cookie /// <summary> asp.net操作cookie /// 创建或修改COOKIE对象并赋Value值 asp.net操作cookie /// </summary> asp.net操作cookie asp.net操作cookie /// <param name="strCookieName">COOKIE对象名</param> asp.net操作cookie /// <param name="strValue">COOKIE对象Value值</param> asp.net操作cookie /// <remarks> asp.net操作cookie /// 对COOKIE修改必须重新设Expires asp.net操作cookie /// </remarks> asp.net操作cookie asp.net操作cookie public static void SetObject(string strCookieName, string strValue) asp.net操作cookie { asp.net操作cookie SetObject(strCookieName, 1, strValue); asp.net操作cookie } asp.net操作cookie asp.net操作cookie asp.net操作cookie /// <summary> asp.net操作cookie /// 创建或修改COOKIE对象并赋Value值 asp.net操作cookie /// </summary> asp.net操作cookie asp.net操作cookie /// <param name="strCookieName">COOKIE对象名</param> asp.net操作cookie /// <param name="iExpires"> asp.net操作cookie /// COOKIE对象有效时间(秒数) asp.net操作cookie /// 1表示永久有效,0和负数都表示不设有效时间 asp.net操作cookie /// 大于等于2表示具体有效秒数 asp.net操作cookie /// 86400秒 = 1天 = (60*60*24) asp.net操作cookie /// 604800秒 = 1周 = (60*60*24*7) asp.net操作cookie /// 2593000秒 = 1月 = (60*60*24*30) asp.net操作cookie /// 31536000秒 = 1年 = (60*60*24*365) asp.net操作cookie /// </param> asp.net操作cookie /// <param name="strValue">COOKIE对象Value值</param> asp.net操作cookie /// <remarks> asp.net操作cookie /// 对COOKIE修改必须重新设Expires asp.net操作cookie /// </remarks> asp.net操作cookie asp.net操作cookie public static void SetObject(string strCookieName, int iExpires, string strValue) asp.net操作cookie { asp.net操作cookie HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); asp.net操作cookie objCookie.Value = HttpContext.Current.Server.UrlEncode(strValue.Trim()); asp.net操作cookie asp.net操作cookie if (iExpires > 0) asp.net操作cookie { asp.net操作cookie if (iExpires == 1) asp.net操作cookie { asp.net操作cookie objCookie.Expires = DateTime.MaxValue; asp.net操作cookie } asp.net操作cookie else asp.net操作cookie { asp.net操作cookie objCookie.Expires = DateTime.Now.AddSeconds(iExpires); asp.net操作cookie } asp.net操作cookie } asp.net操作cookie HttpContext.Current.Response.Cookies.Add(objCookie); asp.net操作cookie } asp.net操作cookie asp.net操作cookie asp.net操作cookie asp.net操作cookie /// <summary> asp.net操作cookie /// 创建COOKIE对象并赋多个KEY键值 asp.net操作cookie /// </summary> asp.net操作cookie asp.net操作cookie /// <param name="strCookieName">COOKIE对象名</param> asp.net操作cookie /// <param name="iExpires"> asp.net操作cookie /// COOKIE对象有效时间(秒数) asp.net操作cookie /// </param> asp.net操作cookie /// <param name="KeyValue">键/值对集合</param> asp.net操作cookie asp.net操作cookie public static void SetObject(string strCookieName, int iExpires, NameValueCollection KeyValue) asp.net操作cookie { asp.net操作cookie HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); asp.net操作cookie asp.net操作cookie foreach (string key in KeyValue.AllKeys) asp.net操作cookie { asp.net操作cookie objCookie[key] = HttpContext.Current.Server.UrlEncode(KeyValue[key].Trim()); asp.net操作cookie } asp.net操作cookie if (iExpires > 0) asp.net操作cookie { asp.net操作cookie if (iExpires == 1) asp.net操作cookie { asp.net操作cookie objCookie.Expires = DateTime.MaxValue; asp.net操作cookie } asp.net操作cookie else asp.net操作cookie { asp.net操作cookie objCookie.Expires = DateTime.Now.AddSeconds(iExpires); asp.net操作cookie } asp.net操作cookie } asp.net操作cookie HttpContext.Current.Response.Cookies.Add(objCookie); asp.net操作cookie } asp.net操作cookie asp.net操作cookie asp.net操作cookie asp.net操作cookie /// <summary> asp.net操作cookie /// 创建或修改COOKIE对象并赋Value值 asp.net操作cookie /// </summary> asp.net操作cookie /// <param name="strCookieName">COOKIE对象名</param> asp.net操作cookie /// <param name="iExpires"> asp.net操作cookie /// COOKIE对象有效时间(秒数) asp.net操作cookie /// </param> asp.net操作cookie /// <param name="strDomain">作用域</param> asp.net操作cookie /// <param name="strValue">COOKIE对象Value值</param> asp.net操作cookie /// <remarks> asp.net操作cookie /// 对COOKIE修改必须重新设Expires asp.net操作cookie /// </remarks> asp.net操作cookie asp.net操作cookie public static void SetObject(string strCookieName, int iExpires, string strValue, string strDomain) asp.net操作cookie { asp.net操作cookie HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); asp.net操作cookie objCookie.Value = HttpContext.Current.Server.UrlEncode(strValue.Trim()); asp.net操作cookie objCookie.Domain = strDomain.Trim(); asp.net操作cookie asp.net操作cookie if (iExpires > 0) asp.net操作cookie { asp.net操作cookie if (iExpires == 1) asp.net操作cookie { asp.net操作cookie objCookie.Expires = DateTime.MaxValue; asp.net操作cookie } asp.net操作cookie else asp.net操作cookie { asp.net操作cookie objCookie.Expires = DateTime.Now.AddSeconds(iExpires); asp.net操作cookie } asp.net操作cookie } asp.net操作cookie HttpContext.Current.Response.Cookies.Add(objCookie); asp.net操作cookie } asp.net操作cookie asp.net操作cookie asp.net操作cookie asp.net操作cookie /// <summary> asp.net操作cookie /// 创建COOKIE对象并赋多个KEY键值 asp.net操作cookie /// </summary> asp.net操作cookie /// <param name="strCookieName">COOKIE对象名</param> asp.net操作cookie /// <param name="iExpires"> asp.net操作cookie /// COOKIE对象有效时间(秒数) asp.net操作cookie /// </param> asp.net操作cookie /// <param name="strDomain">作用域</param> asp.net操作cookie /// <param name="KeyValue">键/值对集合</param> asp.net操作cookie asp.net操作cookie public static void SetObject(string strCookieName, int iExpires, NameValueCollection KeyValue, string strDomain) asp.net操作cookie asp.net操作cookie { asp.net操作cookie asp.net操作cookie HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); asp.net操作cookie foreach (string key in KeyValue.AllKeys) asp.net操作cookie { asp.net操作cookie objCookie[key] = HttpContext.Current.Server.UrlEncode(KeyValue[key].Trim()); asp.net操作cookie } asp.net操作cookie objCookie.Domain = strDomain.Trim(); asp.net操作cookie if (iExpires > 0) asp.net操作cookie { asp.net操作cookie if (iExpires == 1) asp.net操作cookie { asp.net操作cookie objCookie.Expires = DateTime.MaxValue; asp.net操作cookie } asp.net操作cookie else asp.net操作cookie { asp.net操作cookie objCookie.Expires = DateTime.Now.AddSeconds(iExpires); asp.net操作cookie } asp.net操作cookie } asp.net操作cookie HttpContext.Current.Response.Cookies.Add(objCookie); asp.net操作cookie } asp.net操作cookie asp.net操作cookie asp.net操作cookie asp.net操作cookie /// <summary> asp.net操作cookie /// 读取Cookie某个对象的Value值,返回Value值 asp.net操作cookie /// </summary> asp.net操作cookie /// <param name="strCookieName">Cookie对象名称</param> asp.net操作cookie /// <returns>Value值,如果对象本就不存在,则返回 null</returns> asp.net操作cookie asp.net操作cookie public staticstring GetValue(string strCookieName) asp.net操作cookie{ asp.net操作cookieif (HttpContext.Current.Request.Cookies[strCookieName] ==null) asp.net操作cookie{ asp.net操作cookiereturnnull; }asp.net操作cookieasp.net操作cookieelseasp.net操作cookie{ asp.net操作cookiereturn HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[strCookieName].Value); }asp.net操作cookie }asp.net操作cookieasp.net操作cookieasp.net操作cookieasp.net操作cookieasp.net操作cookie///<summary>asp.net操作cookie/// 读取Cookie某个对象的某个Key键的键值 asp.net操作cookie///</summary>asp.net操作cookie///<param name="strCookieName">Cookie对象名称</param>asp.net操作cookie///<param name="strKeyName">Key键名</param>asp.net操作cookie///<returns>Key键值,如果对象或键值就不存在,则返回 null</returns> asp.net操作cookieasp.net操作cookiepublicstaticstring GetValue(string strCookieName, string strKeyName) asp.net操作cookie{ asp.net操作cookieif (HttpContext.Current.Request.Cookies[strCookieName] ==null) asp.net操作cookie{ asp.net操作cookiereturnnull; }asp.net操作cookieasp.net操作cookieelseasp.net操作cookie{ asp.net操作cookiestring strObjValue = HttpContext.Current.Request.Cookies[strCookieName].Value; asp.net操作cookiestring strKeyName2 = strKeyName +"="; asp.net操作cookieasp.net操作cookieif (strObjValue.IndexOf(strKeyName2) ==-1) asp.net操作cookie{ asp.net操作cookiereturnnull; }asp.net操作cookieasp.net操作cookieelseasp.net操作cookie{ asp.net操作cookiereturn HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[strCookieName][strKeyName]); }asp.net操作cookie }asp.net操作cookie }asp.net操作cookieasp.net操作cookieasp.net操作cookieasp.net操作cookieasp.net操作cookie///<summary>asp.net操作cookie/// 修改某个COOKIE对象某个Key键的键值或给某个COOKIE对象添加Key键 asp.net操作cookie///</summary>asp.net操作cookieasp.net操作cookie///<param name="strCookieName">Cookie对象名称</param>asp.net操作cookie///<param name="strKeyName">Key键名</param>asp.net操作cookie///<param name="KeyValue">Key键值</param>asp.net操作cookie///<param name="iExpires">asp.net操作cookie/// COOKIE对象有效时间(秒数) asp.net操作cookie///</param>asp.net操作cookie///<returns>如果对象本就不存在,则返回 false</returns> asp.net操作cookieasp.net操作cookiepublicstaticbool Edit(string strCookieName, string strKeyName, string KeyValue, int iExpires) asp.net操作cookie{ asp.net操作cookieif (HttpContext.Current.Request.Cookies[strCookieName] ==null) asp.net操作cookie{ asp.net操作cookiereturnfalse; }asp.net操作cookieasp.net操作cookieelseasp.net操作cookie{ HttpCookie objCookie asp.net操作cookie= HttpContext.Current.Request.Cookies[strCookieName]; objCookie[strKeyName] asp.net操作cookie= HttpContext.Current.Server.UrlEncode(KeyValue.Trim()); asp.net操作cookieasp.net操作cookieif (iExpires >0) asp.net操作cookie{ asp.net操作cookieif (iExpires ==1) asp.net操作cookie{ objCookie.Expires asp.net操作cookie= DateTime.MaxValue; }asp.net操作cookieasp.net操作cookieelseasp.net操作cookie{ objCookie.Expires asp.net操作cookie= DateTime.Now.AddSeconds(iExpires); }asp.net操作cookie }asp.net操作cookie HttpContext.Current.Response.Cookies.Add(objCookie); asp.net操作cookieasp.net操作cookiereturntrue; }asp.net操作cookieasp.net操作cookie }asp.net操作cookieasp.net操作cookieasp.net操作cookieasp.net操作cookieasp.net操作cookie///<summary>asp.net操作cookie/// 删除COOKIE对象 asp.net操作cookie///</summary>asp.net操作cookie///<param name="strCookieName">Cookie对象名称</param> asp.net操作cookieasp.net操作cookiepublicstaticvoid Delete(string strCookieName) asp.net操作cookie{ HttpCookie objCookie asp.net操作cookie=new HttpCookie(strCookieName.Trim()); objCookie.Expires asp.net操作cookie= DateTime.Now.AddYears(-5); HttpContext.Current.Response.Cookies.Add(objCookie); }asp.net操作cookieasp.net操作cookieasp.net操作cookieasp.net操作cookieasp.net操作cookieasp.net操作cookie///<summary>asp.net操作cookie/// 删除某个COOKIE对象某个Key子键 asp.net操作cookie///</summary>asp.net操作cookieasp.net操作cookie///<param name="strCookieName">Cookie对象名称</param>asp.net操作cookie///<param name="strKeyName">Key键名</param>asp.net操作cookieasp.net操作cookie///<param name="iExpires">asp.net操作cookie/// COOKIE对象有效时间(秒数) asp.net操作cookie///</param>asp.net操作cookie///<returns>如果对象本就不存在,则返回 false</returns> asp.net操作cookieasp.net操作cookiepublicstaticbool Delete(string strCookieName, string strKeyName, int iExpires) asp.net操作cookie{ asp.net操作cookieif (HttpContext.Current.Request.Cookies[strCookieName] ==null) asp.net操作cookie{ asp.net操作cookiereturnfalse; }asp.net操作cookieasp.net操作cookieelseasp.net操作cookie{ HttpCookie objCookie asp.net操作cookie= HttpContext.Current.Request.Cookies[strCookieName]; objCookie.Values.Remove(strKeyName); asp.net操作cookieasp.net操作cookieif (iExpires >0) asp.net操作cookie{ asp.net操作cookieif (iExpires ==1) asp.net操作cookie{ objCookie.Expires asp.net操作cookie= DateTime.MaxValue; }asp.net操作cookieasp.net操作cookieelseasp.net操作cookie{ objCookie.Expires asp.net操作cookie= DateTime.Now.AddSeconds(iExpires); }asp.net操作cookie }asp.net操作cookie HttpContext.Current.Response.Cookies.Add(objCookie); asp.net操作cookieasp.net操作cookiereturntrue; }asp.net操作cookie }asp.net操作cookieasp.net操作cookie#endregion }asp.net操作cookieasp.net操作cookie }asp.net操作cookie

你可能感兴趣的:(asp.net)