.Net中cookie的使用

 

 

Cookie是一段文本信息,在客户端存储 Cookie 是 ASP.NET 的会话状态将请求与会话关联的方法之一。Cookie 也可以直接用于在请求之间保持数据,但数据随后将存储在客户端并随每个请求一起发送到服务器。浏览器对 Cookie 的大小有限制,因此,只有不超过 4096 字节才能保证被接受。

编写Cookie

代码
 
   
1 // 方式1:
2   Response.Cookies[ " username " ].value = " mike " ;
3 Response.Cookies[ " username " ].Expires = DateTime.MaxValue;
4
5   // 方式2:
6 HttpCookie acookie = new HttpCookie( " last " );
7 acookie.Value = " a " ;
8 acookie..Expires = DateTime.MaxValue;
9 Response.Cookies.Add(acookie);
10
11 // 多值Cookie的写法
12
13 // 方式1:
14 Response.Cookies[ " userinfo1 " ][ " name " ].value = " mike " ;
15 Response.Cookies[ " userinfo1 " ][ " last " ].value = " a " ;
16 Response.Cookies[ " userinfo1 " ].Expires = DateTime.MaxValue;
17
18 // 方式2:
19 HttpCookie cookie = new HttpCookie( " userinfo1 " );
20 cookie.Values[ " name " ] = " mike " ;
21 cookie.Values[ " last " ] = " a " ;
22 cookie.Expires = DateTime.MaxValue;
23 // cookie.Expires = System.DateTime.Now.AddDays(1); // 设置过期时间 1天
24 Response.Cookies.Add(cookie);
25
26

 

运行此代码时,可看到一个名为“ASP.NET_SessionId”的Cookie,ASP.NET用这个 Cookie 来保存您的会话的唯一标识符。

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

代码
 
   
修改cookie
1 Response.Cookies[ " Info " ][ " user " ] = " 2 " ;
2 Response.Cookies[ " Info " ].Expires = DateTime.Now.AddDays( 1 ); 删除cookie下的属性
1 HttpCookie acookie = Request.Cookies[ " Info " ];
2 acookie.Values.Remove( " userid " );
3 acookie.Expires = DateTime.Now.AddDays( 1 );
4 Response.Cookies.Add(acookie); 删除所有cookie,就是设置过期时间为现在就行了
1 int limit = Request.Cookies.Count - 1 ;
2 for ( int i = 0 ;i < limit;i ++ )
3 {
4 acookie = Request.Cookies(i)
5 acookie.Expires = DateTime.Now.AddDays( - 1 )
6 Response.Cookies.Add(acookie)
7 }

 

 



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

 

 
  
HttpCookie cookie = new HttpCookie( " userinfo1 " );
cookie.Expires
= DateTime.Now.AddDays( - 30 );
Response.Cookies.Add(cookie);

 

 

 

读取Cookie
Internet Explorer 将站点的 Cookie 保存在文件名格式为 <user>@<domain>.txt 的文件中,其中 <user> 是您的帐户名。
注意:在获取Cookie的值之前,应该确保该 Cookie 确实存在。否则,您将得到一个异常

 

代码
 
   
If (Request.Cookies[ " userName " ] != null )
{
string str = Request.Cookies( " userName " ).Value;
}

// 多值Cookie的读取
If ( Request.Cookies[ " userInfo1 " ] != null )
{
string name = Request.Cookies[ " userInfo1 " ][ " name " ];
string last = Request.Cookies[ " userInfo1 " ][ " last " ];
}


// 读取 Cookie 集合
for ( int i = 0 ;i < Request.Cookies.Count ;i ++ )
{
HttpCookie cookies
= Request.Cookies;
Response.Write(
" name= " + cookies.Mame + " <br/> " );
if (cookies.HasKeys ) // 是否有子键
{
System.Collections.Specialized.NameValueCollection NameColl
= aCookie.Values ;
for ( int j = 0 ;j < NameColl.Count;j ++ )
{
Response.Write(
" 子键名= " + NameColl.AllKey[j] + " <br/> " );
Response.Write(
" 子键值= " + NameColl[j] + " <br/> " );
}

}
else
{
Response.Write(
" value= " + cookies.Value + " <br/> " );
}
}

 

 

 

代码
 
   
// ----------写入Cookie
HttpCookie cookie = new HttpCookie( " userinfo1 " );
cookie.Expires
= System.DateTime.Now.AddMinutes( 2 ); // 设置过期时间
for ( int i = 0 ; i < 20 ; i ++ )
{

cookie.Values[
" Val " + i.ToString()] = i.ToString();
Response.Cookies.Add(cookie);
}

// 读取 Cookie 集合
for ( int i = 0 ; i < Request.Cookies.Count; i ++ )
{
if (Request.Cookies.AllKeys[i] == " userinfo1 " )
{

HttpCookie cookies
= Request.Cookies[ " userInfo1 " ];
Response.Write(
" name= " + cookies.Name + " <br/> " );
// Response.Write("name=" + cookies.Value + "<br/>");

if (cookies.HasKeys) // 是否有子键
{
System.Collections.Specialized.NameValueCollection NameColl
= cookies.Values;
for ( int j = 0 ; j < NameColl.Count; j ++ )
{
Response.Write(
" 子键名= " + NameColl.AllKeys[j] + " <br/> " );
Response.Write(
" 子键值= " + NameColl[j] + " <br/> " );
}

}
else
{
Response.Write(
" value= " + cookies.Value + " <br/> " );
}
}

}

/// //********************************************************************

 

 

 

 

 

转载于:https://www.cnblogs.com/baogg/articles/1860789.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值