protected
void
Button2_Click(
object
sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie( " MyCook " ); // 初使化并设置Cookie的名称
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan( 0 , 0 , 1 , 0 , 0 ); // 过期时间为1分钟
cookie.Expires = dt.Add(ts); // 设置过期时间
cookie.Values.Add( " userid " , " userid_value " );
cookie.Values.Add( " userid2 " , " userid2_value2 " );
Response.AppendCookie(cookie);
// 输出该Cookie的所有内容
// Response.Write(cookie.Value); // 输出为:userid=userid_value&userid2=userid2_value2
}
// 读取
protected void Button1_Click( object sender, EventArgs e)
{
// HttpCookie cokie = new HttpCookie("MyCook"); // 初使化
if (Request.Cookies[ " MyCook " ] != null )
{
// Response.Write("Cookie中键值为userid的值:" + Request.Cookies["MyCook"]["userid"]); // 整行
// Response.Write("Cookie中键值为userid2的值" + Request.Cookies["MyCook"]["userid2"]);
Response.Write(Request.Cookies[ " MyCook " ].Value); // 输出全部的值
}
}
// 修改Cookie
protected void Button3_Click( object sender, EventArgs e)
{
// 获取客户端的Cookie对象
HttpCookie cok = Request.Cookies[ " MyCook " ];
if (cok != null )
{
// 修改Cookie的两种方法
cok.Values[ " userid " ] = " alter-value " ;
cok.Values.Set( " userid " , " alter-value " );
// 往Cookie里加入新的内容
cok.Values.Set( " newid " , " newValue " );
Response.AppendCookie(cok);
}
}
// 删除Cookie
protected void Button4_Click( object sender, EventArgs e)
{
HttpCookie cok = Request.Cookies[ " MyCook " ];
if (cok != null )
{
if ( ! CheckBox1.Checked)
{
cok.Values.Remove( " userid " ); // 移除键值为userid的值
}
else
{
TimeSpan ts = new TimeSpan( - 1 , 0 , 0 , 0 );
cok.Expires = DateTime.Now.Add(ts); // 删除整个Cookie,只要把过期时间设置为现在
}
Response.AppendCookie(cok);
}
}
以前写程序都没去关注Cookie的应用,因为ASP.NET在客户端保存数据的方式比较多,且以前写的程序都没有用到Cookie,所以就没去了解,今天有个程序总是用Cookie出错。现在在网上查查Cookie的常用方法。
Cookie的用法也和ASP中差不多。比如我们建立一个名为user,值为111的cookie
HttpCookie cookie = new HttpCookie( " user " );
cookie.Value = " 111 " ;
Response.AppendCookie(cookie);
我们取出Cookie值也很简单
HttpCookie cookie = Request.Cookies[ " user " ];
cookieValue = cookie.Value;
有时候我们想在一个Cookie中储存多个信息,那也没有问题。比如我们在名为user的cookie下加多个信息
HttpCookie cookie = new HttpCookie( " user " );
cookie.Values.Add( " name " , " 222 " );
cookie.Values.Add( " pwd " , " 333 " );
cookie.Values.Add( " member " , " admin " );
Response.AppendCookie(cookie);
从客户端获取取信息也一样简单
HttpCookie cookie = Request.Cookies[ " user " ];
value1 = cookies.Values[ " name " ];
value2 = cookies.Values[ " pwd " ];
方法二、
写入:
代码如下:
HttpCookie cookie = new HttpCookie( " id_admin_ " );
cookie.Value = model.id_admin_.ToString();
// cookie.Domain = ".sosuo8.com";
HttpContext.Current.Response.Cookies.Add(cookie);
cookie = new HttpCookie( " name_admin_ " );
// 可能是中文字符,必须经过编码
cookie.Value = HttpUtility.UrlEncode(model.name_admin_);
// cookie.Domain = ".sosuo8.com";
HttpContext.Current.Response.Cookies.Add(cookie);
cookie = new HttpCookie( " guid " );
cookie.Value = Guid.NewGuid().ToString();
// cookie.Domain = ".sosuo8.com";
HttpContext.Current.Response.Cookies.Add(cookie);
读取:
代码如下:
HttpContext.Current.Request.Cookies[ " guid " ].Value
asp.net清空cookie 清空单个
代码如下:
Response.Cookies[ " admin " ].Expires = DateTime.Now.AddDays( - 1 );
asp.net清空cookie 清空所有
Request.Cookies.Clear()这个方法并不是删除Cookie
删除 Cookie(即从用户的硬盘中物理移除 Cookie)是修改 Cookie 的一种形式。
由于 Cookie 在用户的计算机中,因此无法将其直接移除。
但是,可以让浏览器来为您删除 Cookie。
该技术是创建一个与要删除的 Cookie 同名的新 Cookie,
并将该 Cookie 的到期日期设置为早于当前日期的某个日期。
当浏览器检查 Cookie 的到期日期时,浏览器便会丢弃这个现已过期的 Cookie。
下面的代码示例演示删除应用程序中所有可用 Cookie 的一种方法:
代码如下:
HttpCookie aCookie;
string cookieName;
int limit = Request.Cookies.Count;
for ( int i = 0 ; i < limit; i ++ )
{
cookieName = Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
aCookie.Expires = DateTime.Now.AddDays( - 1 );
Response.Cookies.Add(aCookie);
}
{
HttpCookie cookie = new HttpCookie( " MyCook " ); // 初使化并设置Cookie的名称
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan( 0 , 0 , 1 , 0 , 0 ); // 过期时间为1分钟
cookie.Expires = dt.Add(ts); // 设置过期时间
cookie.Values.Add( " userid " , " userid_value " );
cookie.Values.Add( " userid2 " , " userid2_value2 " );
Response.AppendCookie(cookie);
// 输出该Cookie的所有内容
// Response.Write(cookie.Value); // 输出为:userid=userid_value&userid2=userid2_value2
}
// 读取
protected void Button1_Click( object sender, EventArgs e)
{
// HttpCookie cokie = new HttpCookie("MyCook"); // 初使化
if (Request.Cookies[ " MyCook " ] != null )
{
// Response.Write("Cookie中键值为userid的值:" + Request.Cookies["MyCook"]["userid"]); // 整行
// Response.Write("Cookie中键值为userid2的值" + Request.Cookies["MyCook"]["userid2"]);
Response.Write(Request.Cookies[ " MyCook " ].Value); // 输出全部的值
}
}
// 修改Cookie
protected void Button3_Click( object sender, EventArgs e)
{
// 获取客户端的Cookie对象
HttpCookie cok = Request.Cookies[ " MyCook " ];
if (cok != null )
{
// 修改Cookie的两种方法
cok.Values[ " userid " ] = " alter-value " ;
cok.Values.Set( " userid " , " alter-value " );
// 往Cookie里加入新的内容
cok.Values.Set( " newid " , " newValue " );
Response.AppendCookie(cok);
}
}
// 删除Cookie
protected void Button4_Click( object sender, EventArgs e)
{
HttpCookie cok = Request.Cookies[ " MyCook " ];
if (cok != null )
{
if ( ! CheckBox1.Checked)
{
cok.Values.Remove( " userid " ); // 移除键值为userid的值
}
else
{
TimeSpan ts = new TimeSpan( - 1 , 0 , 0 , 0 );
cok.Expires = DateTime.Now.Add(ts); // 删除整个Cookie,只要把过期时间设置为现在
}
Response.AppendCookie(cok);
}
}
以前写程序都没去关注Cookie的应用,因为ASP.NET在客户端保存数据的方式比较多,且以前写的程序都没有用到Cookie,所以就没去了解,今天有个程序总是用Cookie出错。现在在网上查查Cookie的常用方法。
Cookie的用法也和ASP中差不多。比如我们建立一个名为user,值为111的cookie
HttpCookie cookie = new HttpCookie( " user " );
cookie.Value = " 111 " ;
Response.AppendCookie(cookie);
我们取出Cookie值也很简单
HttpCookie cookie = Request.Cookies[ " user " ];
cookieValue = cookie.Value;
有时候我们想在一个Cookie中储存多个信息,那也没有问题。比如我们在名为user的cookie下加多个信息
HttpCookie cookie = new HttpCookie( " user " );
cookie.Values.Add( " name " , " 222 " );
cookie.Values.Add( " pwd " , " 333 " );
cookie.Values.Add( " member " , " admin " );
Response.AppendCookie(cookie);
从客户端获取取信息也一样简单
HttpCookie cookie = Request.Cookies[ " user " ];
value1 = cookies.Values[ " name " ];
value2 = cookies.Values[ " pwd " ];
方法二、
写入:
代码如下:
HttpCookie cookie = new HttpCookie( " id_admin_ " );
cookie.Value = model.id_admin_.ToString();
// cookie.Domain = ".sosuo8.com";
HttpContext.Current.Response.Cookies.Add(cookie);
cookie = new HttpCookie( " name_admin_ " );
// 可能是中文字符,必须经过编码
cookie.Value = HttpUtility.UrlEncode(model.name_admin_);
// cookie.Domain = ".sosuo8.com";
HttpContext.Current.Response.Cookies.Add(cookie);
cookie = new HttpCookie( " guid " );
cookie.Value = Guid.NewGuid().ToString();
// cookie.Domain = ".sosuo8.com";
HttpContext.Current.Response.Cookies.Add(cookie);
读取:
代码如下:
HttpContext.Current.Request.Cookies[ " guid " ].Value
asp.net清空cookie 清空单个
代码如下:
Response.Cookies[ " admin " ].Expires = DateTime.Now.AddDays( - 1 );
asp.net清空cookie 清空所有
Request.Cookies.Clear()这个方法并不是删除Cookie
删除 Cookie(即从用户的硬盘中物理移除 Cookie)是修改 Cookie 的一种形式。
由于 Cookie 在用户的计算机中,因此无法将其直接移除。
但是,可以让浏览器来为您删除 Cookie。
该技术是创建一个与要删除的 Cookie 同名的新 Cookie,
并将该 Cookie 的到期日期设置为早于当前日期的某个日期。
当浏览器检查 Cookie 的到期日期时,浏览器便会丢弃这个现已过期的 Cookie。
下面的代码示例演示删除应用程序中所有可用 Cookie 的一种方法:
代码如下:
HttpCookie aCookie;
string cookieName;
int limit = Request.Cookies.Count;
for ( int i = 0 ; i < limit; i ++ )
{
cookieName = Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
aCookie.Expires = DateTime.Now.AddDays( - 1 );
Response.Cookies.Add(aCookie);
}