c#和net存取cookies操作示例
作者:
cookies的创建:
在客户端创建一个username的cookies,其值为oneday,有效期为1天.
方法1:
Response.Cookies["username"].Value="admin";
Response.Cookies["username"].Expires=DateTime.Now.AddDays(1);
方法2:
System.Web.HttpCookie newcookie=new HttpCookie("username");
newcookie.Value="oneday";
newcookie.Expires=DateTime.Now.AddDays(1);
Response.AppendCookie(newcookie);
创建带有子键的cookies:
System.Web.HttpCookie newcookie=new HttpCookie("user");
newcookie.Values["username"]="admin";
newcookie.Values["password"]="admin";
newcookie.Expires=DateTime.Now.AddDays(1);
Response.AppendCookie(newcookie);
cookies的读取:
无子键读取:
if(Request.Cookies["username"]!=null)
{
Response.Write(Server.HtmlEncode(Request.Cookies["username"].Value));
}
有子键读取:
if(Request.Cookies["user"]!=null)
{
Response.Write(Server.HtmlEncode(Request.Cookies["user"]["username"].Value));
Response.Write(Server.HtmlEncode(Request.Cookies["user"]["password"].Value));
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class Cookie
{
/// <summary>
/// Cookies赋值
/// </summary>
/// <param name="strName">主键</param>
/// <param name="strValue">键值</param>
/// <param name="strDay">有效天数</param>
/// <returns></returns>
public bool setCookie(string strName, string strValue, int strDay)
{
try
{
HttpCookie Cookie = new HttpCookie(strName);
//Cookie.Domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
Cookie.Expires = DateTime.Now.AddDays(strDay);
Cookie.Value = strValue;
System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 读取Cookies
/// </summary>
/// <param name="strName">主键</param>
/// <returns></returns>
public string getCookie(string strName)
{
HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
if (Cookie != null)
{
return Cookie.Value.ToString();
}
else
{
return null;
}
}
/// <summary>
/// 删除Cookies
/// </summary>
/// <param name="strName">主键</param>
/// <returns></returns>
public bool delCookie(string strName)
{
try
{
HttpCookie Cookie = new HttpCookie(strName);
//Cookie.Domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
Cookie.Expires = DateTime.Now.AddDays(-1);
System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
return true;
}
catch
{
return false;
}
}
}
示例:
Cookie Cookie = new Cookie();
Cookie.setCookie("name", "aaa",1);//赋值
Cookie.getCookie("name");//取值
Cookie.delCookie("name");//删除
注意:当Cookie存中文出现乱码,则在存放时给中文编码,如Cookie.setCookie("name", Server.UrlEncode("aaa"),1),读取时解码即可
另外:只要不给cookie设置过期时间,cookie在浏览器关闭的时候自动失效
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class Cookie
{
/// <summary>
/// Cookies赋值
/// </summary>
/// <param name="strName">主键</param>
/// <param name="strValue">键值</param>
/// <param name="strDay">有效天数</param>
/// <returns></returns>
public bool setCookie(string strName, string strValue, int strDay)
{
try
{
HttpCookie Cookie = new HttpCookie(strName);
//Cookie.Domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
Cookie.Expires = DateTime.Now.AddDays(strDay);
Cookie.Value = strValue;
System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 读取Cookies
/// </summary>
/// <param name="strName">主键</param>
/// <returns></returns>
public string getCookie(string strName)
{
HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
if (Cookie != null)
{
return Cookie.Value.ToString();
}
else
{
return null;
}
}
/// <summary>
/// 删除Cookies
/// </summary>
/// <param name="strName">主键</param>
/// <returns></returns>
public bool delCookie(string strName)
{
try
{
HttpCookie Cookie = new HttpCookie(strName);
//Cookie.Domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
Cookie.Expires = DateTime.Now.AddDays(-1);
System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
return true;
}
catch
{
return false;
}
}
}
示例:
Cookie Cookie = new Cookie();
Cookie.setCookie("name", "aaa",1);//赋值
Cookie.getCookie("name");//取值
Cookie.delCookie("name");//删除
注意:当Cookie存中文出现乱码,则在存放时给中文编码,如Cookie.setCookie("name", Server.UrlEncode("aaa"),1),读取时解码即可
另外:只要不给cookie设置过期时间,cookie在浏览器关闭的时候自动失效
您可能感兴趣的文章:
- C#基于cookie实现的购物车功能
- C#基于WebBrowser获取cookie的实现方法
- C#中Cookie之存储对象
- C#自定义简化cookie类实例
- C#中Request.Cookies 和 Response.Cookies 的区别分析
- C# javascript 读写Cookie的方法
- C#中的cookie编程简单实例与说明
- c# 获取CookieContainer的所有cookies函数代码
- 基于C#后台调用跨域MVC服务及带Cookie验证的实现
- c# 对cookies(增、删、改、查)的操作方法
- C# HttpClient Cookie验证解决方法
- asp.net(C#)跨域及跨域写Cookie问题
- c# .net在WEB页中的COOKIES设置技巧
- 总结C#网络编程中对于Cookie的设定要点