COOKIE programming in C #

xiaoxiao2021-03-06  64

Cookie is the so-called "sweet cookie", he earliest is in Netscape Navigator 2.0. Cookie is actually created by the web server, and stores the information on the computer on the computer. So why is the web server to create such a file on the client? This is because when the client sends a request to the web server (for example, when you are ready to browse the page), whether this client is a first visit, the server treats it as the first time, the WEB server does Just simply respond, then turn off the connection with the user. This is obvious when the disadvantage caused by the process. Since the network view company has developed a cookie, you can use the cookie to save the user's identification information. The role of cookie can record the page that you have visited on the site, which helps you customize when you next accessing the site. Cookies can also store individual identifiable information. Personal recognition information is available to identify or contact information, such as names, email addresses, home or work addresses, or phone numbers. However, the website can only access the individual identifiable information you provide. For example, the website will not be able to determine your email name unless you provide an email name. In addition, the website cannot access additional information on your computer through cookies. Of course, unless you are provided. So where is the cookie to store? If the system's system is Window 98 and installed in the "C" disk, the cookie is stored in the "C: / Windows / Cookies" directory; if the machine system is the window 2000 and installed in the "C" disk, then cookie is stored "C: / documents and settings / administrator / cookies" directory. Understand so many knowledge of cookies, let's take a look at how the focus of this article - how COOKIE is programmed. The main contents are two: one is how C # is how to write cookies; the second is how C # is how to access the cookies written.

First, the program design and running software environment introduced in this article:

Microsoft Window 2000 Server Edition .NET Framework SDK Beta 2

C # performs cookie aspects to be implemented by the ASP.NET page.

Second, C # How to write cookie:

In order to write cookies, his steps have three steps, as follows:

First create an HTTPCOOKIE object, constructed a cookie through this object, the name of this object is the Cookie name generated later. The specific code: httpcookie cookie = new httpcookie ("User-defined Cookie Name");

Then the "value" property of the created HTTPCookie object is assigned a string value, "value" is the value of the cookie generated later. The specific code is as follows: cookie.Value = "Users to cookie assignment"; if you want to write the cookie value is not a simple string, but a complex data type, we know that these data types cannot be stored directly to cookie In the cookie, you can only store strings. But you can pass a way to convert this complex data type into multiple strings, then assign the multiple strings to the resulting cookie value, so the content in the cookie is rich, and the cookie is used later. The completion of the function is also powerful. At this time, you may understand why when you browse the web server, the web server will know when you have been viewed, and once you have to wait for information. Because this information has been stored in your first browsing page, the WEB server has a cookie generated. The following code is to store multiple strings to cookie: cookie ["Name"] = "Wang Tian"; cookie ["Gender" = "Men"; cookie ["age" = "26"; cookie Temporary and forever. Permanent cookies are stored on the computer in the form of a file, and when Internet Explorer is retained on the computer. When you access the site again, create the cookie website can be read. When the specific programming is written in this cookie, set the lifecycle of the cookie, the specific code: datetime dtnow = datetime. Now; time; time, Timespan Tsminute = New Timespan (0, 1, 0, 0); cookie. Expires = DTNOW TSMINUTE; The above code is the life period of the created cookie is "an hour", which you can set the specific life of the cookie by modifying the "Timespan" property. Finally, call the "ADD () method of the" response.cookies ", add this object, so you can generate a cookie.

The specific code is as follows: response. Cookies. Add (cookie); the following code is the full code of COOKIE in C #: <% @ language = "c #"%>