六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 37|回复: 0

8、使用浏览器Cookie

[复制链接]

升级  57%

115

主题

115

主题

115

主题

举人

Rank: 3Rank: 3

积分
371
 楼主| 发表于 2013-1-26 14:03:20 | 显示全部楼层 |阅读模式
使用浏览器Cookie

set-cookie:message=hello

注意:
Cookie就是一段文本。Cookie只能用于存储字符串值。

可以创建两种类型的cookie:会话Cookie和持久化Cookie
会话Cookie只存于内存,当用户关闭浏览器时,会话Cookie就永远消失了。
持久化Cookie可以存在几个月甚至几年。持久化Cookie创建后,会被浏览器长久地保存在用户的电脑上。
\Documents and Settings\[user]\Cookies


1、Cookie的安全性限制
首先,所有的Cookie是与域名相关的
其次,浏览器存储Cookie的重要限制是其大小的限制 不能超过4096字节(包含所有的Cookie名称和值在内)
最后,大多数浏览器都限制可以被设置的cookie数量,一个域名不超过20个cookie (IE除外)

2、创建Cookie
Response.Cookies 集合添加Cookie
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    protected void btnAdd_Click(object sender, EventArgs e)    {        Response.Cookies["message"].Value = txtCookieValue.Text;    }</script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        <asp:Label ID="lblCookieValue" runat="server" AssociatedControlID="txtCookieValue"            Text="Cookie Value"></asp:Label>        <asp:TextBox ID="txtCookieValue" runat="server"></asp:TextBox>        <asp:Button ID="btnAdd" runat="server" Text="Add Value"  />    </div>    </form></body></html>
Cookie的名称是区分大小写的。
2011-5-11 11:48 danny

创建持久化的Cookie
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    protected void Page_Load(object sender, EventArgs e)    {        int counter = 0;        if (Request.Cookies["counter"] != null)            counter = Int32.Parse(Request.Cookies["counter"].Value);        counter++;        Response.Cookies["counter"].Value = counter.ToString();        Response.Cookies["counter"].Expires = DateTime.Now.AddYears(2);        lblCounter.Text = counter.ToString();    }</script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        You have visited this page        <asp:Label ID="lblCounter" runat="server" Text="Label"></asp:Label>        times!    </div>    </form></body></html>
Response.Cookies["counter"].Expires = DateTime.Now.AddYears(2);
设为两年。当为某个Cookie设置了过期时间后,该Cookie就被保存为持久化cookie了。

3、读取Cookie
可以使用Response.Cookies集合创建和修改Cookie,
可以使用Request.Cookies集合读取Cookie值

GetCookies.aspx
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    protected void Page_Load(object sender, EventArgs e)    {        ArrayList colCookies = new ArrayList();        for (int i = 0; i < Request.Cookies.Count; i++)            colCookies.Add(Request.Cookies[i]);        grdCookies.DataSource = colCookies;        grdCookies.DataBind();    }</script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        <asp:GridView ID="grdCookies" runat="server">        </asp:GridView>    </div>    </form></body></html>

4、设置Cookie属性
Domain 关联到的Cookie的域名。默认当前域名
Expires  指定过期时间
HasKeys  确定该Cookie是否是一个多值cookie
HttpOnly 避免Cookie被JavaScript访问
Name 用户指定Cookie的名称
Path   关联到Cookie的路径。默认值是/
Secure  指定Cookie需要通过SSL连接传递
Value   允许读/写Cookie的值
Values  多值

5、删除Cookie
删除Cookie的方法并不直观。要删除一个存在的Cookie,必须设置其过期时间为一个过去的时间。

DeleteAllCookies.aspx
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    protected void Page_Load(object sender, EventArgs e)    {        string[]             cookies = Request.Cookies.AllKeys;        foreach (string cookie in cookies)        {            BulletedList1.Items.Add("Deleting " + cookie);            Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);        }    }</script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        <asp:BulletedList ID="BulletedList1" EnableViewState="false" runat="server">        </asp:BulletedList>    </div>    </form></body></html>

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        string[]
             cookies = Request.Cookies.AllKeys;
        foreach (string cookie in cookies)
        {
            BulletedList1.Items.Add("Deleting " + cookie);
            Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:BulletedList ID="BulletedList1" EnableViewState="false" runat="server">
        </asp:BulletedList>
    </div>
    </form>
</body>
</html>

2011-5-11 22:20 danny
6、使用多值Cookie
根据Cookie规范,单个域名,浏览器不能存储超过20个Cookie。可以通过创建多值Cookie来超越该限制。
多值Cookie是一个包含子键的Cookie。可以根据需要创建任意数量的子键。

setCookieValues.aspx
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    protected void btnSubmit_Click(object sender, EventArgs e)    {        Response.Cookies["preferences"]["firstName"] = TxtFirstName.Text;        Response.Cookies["preferences"]["lastName"] = txtLastName.Text;        Response.Cookies["preferences"]["favoriteColor"] = txtFavoriteColor.Text;        Response.Cookies["preferences"].Expires = DateTime.MaxValue;    }</script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        <asp:Label ID="lblFirstName" runat="server" AssociatedControlID="TxtFirstName" Text="First Name"></asp:Label>        <br />        <asp:TextBox ID="TxtFirstName" runat="server"></asp:TextBox>        <br />        <asp:Label ID="lblLastName" runat="server" AssociatedControlID="TxtFirstName" Text="Last Name"></asp:Label>        <br />        <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>        <br />        <asp:Label ID="lblFavoriteColor" runat="server" AssociatedControlID="TxtFirstName"            Text="Favorite Color"></asp:Label>        <br />        <asp:TextBox ID="txtFavoriteColor" runat="server"></asp:TextBox>        <br />        <asp:Button ID="btnSubmit" Text="Submit" runat="server"  />    </div>    </form></body></html>

查看
GetCookieValues.aspx
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    protected void Page_Load(object sender, EventArgs e)    {        if (Request.Cookies["preferences"] != null)        {            lblFirstName.Text = Request.Cookies["preferences"]["firstName"];            lblLastName.Text = Request.Cookies["preferences"]["lastName"];            lblFavoriteColor.Text = Request.Cookies["preferences"]["favoriteColor"];        }    }</script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        First Name:        <asp:Label ID="lblFirstName" runat="server"></asp:Label>        <br />        Last Name:        <asp:Label ID="lblLastName" runat="server"></asp:Label>        <br />        Favorite Color:        <asp:Label ID="lblFavoriteColor" runat="server"></asp:Label>        <br />    </div>    </form></body></html>

2011-5-13 10:59 danny
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表