ASP.NET常用技术之Cookie

时间:2021-04-17 09:24:52

cookie是一小段的文本信息(多数浏览器限制cookie最大字节数为4096字节),在浏览器和服务器之间随用户请求而传递(用户访问网站,没有设置cookie限制范围情况下,无论请求那个页面,浏览器和服务器之间都会交换cookie信息)。Cookie是asp.ent实现状态管理的一种方法。

单值Cookie的读写:

Cookie有两种写入的方法:

方法一:直接写入

 Response.Cookies["MyCookieName"].Value = "MyCookieVal";
Response.Cookies["MyCookieName"].Expires = DateTime.Now.AddYears();

方法二:创建cookie对象,将其加入到Cookie集合中去

HttpCookie mycookie = new HttpCookie("MyCookieName");
mycookie.Value = "MyCookieVal";
mycookie.Expires = DateTime.Now.AddYears();
Response.Cookies.Add(mycookie);

Cookie的读取:(读取时一定要判断是否存在,否则会抛出NullReferenceException异常)

        if (Request.Cookies["MyCookieName"] != null)
{
string cookieVale = Response.Cookies["MyCookieName"].Value;
} if (Request.Cookies["MyCookieName"] != null)
{
HttpCookie cookie = Request.Cookies["MyCookieName"];
string cookieValue = cookie.Value;
}

多值Cookie的读写:

写入方法一:直接写入

            Response.Cookies["MyCookieName"]["key1"] = "value1";
Response.Cookies["MyCookieName"]["key2"] = "value2";
Response.Cookies["MyCookieName"].Expires = DateTime.Now.AddDays();

  写入方法二:创建cookie对象,将其加入到Cookie集合中去

            HttpCookie cookie = new HttpCookie("MyCookieName");
cookie.Values["key1"] = "value1";
cookie.Values["key2"] = "value2";
Response.Cookies.Add(cookie);

读取方法一:根据cookie名和键读取

        if (Request.Cookies["MyCookieName"] != null)
{
string cookieVale = Response.Cookies["MyCookieName"]["key1"];
string cookieValue2 = Response.Cookies["MyCookieName"]["key2"];
}

读取方法二:循环读取

if (Request.Cookies["MyCookieName"] != null)
        {
            HttpCookie cookie = Request.Cookies["MyCookieName"];
            if (cookie.HasKeys)
            {
                for (int i = 0; i < cookie.Values.Count; i++)
                {
                    string key=cookie.Values.GetKey(i);
                    string value = cookie.Values[i];
                }
            }
            else
            {
                string value = cookie.Value;
            }
        }

Cookie的范围控制

Cookie的范围控制涉及到两个属性:PathDomain

1.Path 设置Path属性可以使Cookie只允许指定的路劲或者应用程序来访问。

        Response.Cookies["MyCookieName"].Value = "val";
Response.Cookies["MyCookieName"].Path = "/test";
Response.Cookies["MyCookieName"].Expires = DateTime.Now.AddDays();

经过Path设置后 只有该网站的test目录下的页面以及该文件夹下的所有页面可以访问该cookie.比如:www.ssss.com/test/ddd.aspx

2.Domain 设置Domain属后只有指定域可以访问cookie

        Response.Cookies["MyCookieName"].Value = "val";
Response.Cookies["MyCookieName"].Domain = "help.test.com";
Response.Cookies["MyCookieName"].Expires = DateTime.Now.AddDays();

代码中设置cookie的域为"help.test.com",这样,只有类似“help.test.com/dd.aspx”等的页面才能访问cookie,如“support.test.com/ddd.aspx”就不能访问该Cookie了。

Cookie的删除

删除Cookie十分简单,只要把要删除的Cookie的过期时间设置为过期的时间就行

if (Request.Cookies["MyCookieName"]!=null)
        {
            HttpCookie cookie = Request.Cookies["MyCookieName"];//获取要删除的cookie
            cookie.Expires = DateTime.Now.AddDays(-1); //设置过期时间
            Response.Cookies.Add(cookie);
        }

删除多值Cookie的某个键值

       if (Request.Cookies["MyCookieName"]!=null)
{
HttpCookie cookie = Request.Cookies["MyCookieName"];//获取cookie
cookie.Values.Remove("key1"); //删除key1
cookie.Expires = DateTime.Now.AddDays();
Response.Cookies.Add(cookie);
}

关于Expires

Expires是Cookie的过期时间,如果将时间设置为大于当前时间,则Cookie会保存到本地的硬盘上,如果设置时间小于当前时间,则Cookie会从本地硬盘删除,不设置过期时间,服务器依旧会向客户端读写Cookie,但是不会将Cookie保存到本地硬盘。