A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The
cookie contains information the Web application can read whenever the user visits the site.
For example, if a user requests a page from your site and your application sends not just a page, but also a cookie
containing the date and time, when the user's browser gets the page, the browser also gets the cookie, which it stores in a
folder on the user's hard disk.
Later, if user requests a page from your site again, when the user enters the URL the browser looks on the local hard disk
for a cookie associated with the URL. If the cookie exists, the browser sends the cookie to your site along with the page
request. Your application can then determine the date and time that the user last visited the site. You might use the
information to display a message to the user or check an expiration date.
Cookies are associated with a Web site, not with a specific page, so the browser and server will exchange cookie information
no matter what page the user requests from your site. As the user visits different sites, each site might send a cookie to
the user's browser as well; the browser stores all the cookies separately.
Cookies 帮助站点存储访问者的信息. More generally, cookies are one way of maintaining continuity in a Web application—that
is, of performing state management. Except for the brief time when they are actually exchanging information, the browser and
Web server are disconnected. Each request a user makes to a Web server is treated independently of any other request. Many
times, however, it's useful for the Web server to recognize users when they request a page. For example, the Web server on a
shopping site keeps track of individual shoppers so the site can manage shopping carts and other user-specific information.
A cookie therefore acts as a kind of calling card, presenting pertinent identification that helps an application know how to
proceed.
Cookies帮助站点记住用户. For example, a site conducting a poll might use a cookie simply as a Boolean value to indicate
whether a user's browser has already participated in voting so that the user cannot vote twice. A site that asks a user to
log on might use a cookie to record that the user already logged on so that the user does not have to keep entering
credentials.
Cookie为客户端信息存放对象,可以把用户的信息保存在用户本地,不必总是访问服务器
与Session不同,Session为用户全局变量,对于该用户的所有操作过程都有效。
//1。创建Cookie对象
HttpCookie newCookie = new HttpCookie("User");
//2。Cookie中添加信息项:为键值对,key/value
newCookie.Values.Add("Name",tbUserName.Text.Trim());
newCookie.Values.Add("Psd",tbUserPsd.Text.Trim());
//3。如果不设置Expires属性,即为临时Cookie,浏览器关闭即消失
newCookie.Expires = DateTime.Now.AddDays(1); //设置过期天数为1天
//4。写入Cookies集合
Response.AppendCookie(newCookie);
//5。通过Request对象读取得到Cookies的值
HttpCookie newCookie = Request.Cookies["User"];
//6。通过Response对象写入客户端的Cookie
newCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(newCookie);
//设置cookies 1
System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.Cookies["userName"].Value = "patrick";
Response.Cookies["userName"].Expires = timeout;
Response.Cookies["userName"].Domain = "abc.com";
//设置cookies 2
HttpCookie newCookie = new HttpCookie("lastVisit");
newCookie.Value = DateTime.Now.ToString();
newCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(newCookie);
//第一种方式直接把cookies添加到cookies集合。是因为NameObjectCollectionBase类型的特定容器。
//第二种方式通过HttpCookie对象设置属性,然后调用Add方法添加到cookies集合。实例化HttpCookie对象时必须提供cookies名。
//两种方式都把cookies写到了浏览器,而且expiration对象必须是DateTime类型的。
//可以通过设置文件夹地址(path)或域名(domain)来限制访问cookies
appCookie.Path = "/Application1"; //放在了当前路径的子文件夹Application1里
Response.Cookies["domain"].Domain = "contoso.com";
//The cookie will then be available to the primary domain as well as to sales.contoso.com and support.contoso.com domains.
//可以通过HttpRequest对象(或Page类的Request属性)来读取cookies
//读取cookies1
if(Request.Cookies["userName"] != null) //确保cookies存在,否则空引用异常
Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);
//读取cookies2
if(Request.Cookies["userName"] != null)
{
HttpCookie aCookie = Request.Cookies["userName"];
Label1.Text = Server.HtmlEncode(aCookie.Value);
}
//删除cookies。 因为cookies在客户端,不能直接删除。可以创建一个新的和想删除的cookie同名的cookie,然后把expiration设为比今天更
早的时间,cookies就会被删除。
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);
}
本文深入探讨了Cookie在Web应用中的作用,包括其如何存储和传递用户信息,以及如何利用Cookie实现用户状态管理和个性化体验。同时介绍了Cookie的创建、读取、设置过期时间、删除方法,并详细解释了Cookie与其他Web组件的交互。通过实例展示了如何在实际项目中应用Cookie,以提升用户体验和网站功能。
846

被折叠的 条评论
为什么被折叠?



