Use of cookies in ASP. NET (practical tutorial)

Source: Internet
Author: User

It is inevitable to learn about web development and use cookies. For reference only. I personally feel that the use of cookies and ASP. the session in. NET is very similar, except that the cookie is stored on the client and the session is on the server. Both are for recording information and serve as the credential for accessing the website. This article is only a practical operation and does not involve theoretical knowledge.

I wanted to draw a mind map, but I still think about it. I don't like posting a dazzling mind map in my blog, in addition, the existence of mind maps in the form of pictures is not conducive to the spread of knowledge. Before the actual operation, let's talk about cookies!

Cookies are actually some small files stored on the client, saving some information for the next visit to the website, such as whether to log on. The biggest advantage of cookie is that it is easy to use, and the transmission, management, and maintenance are all completed by the browser. The disadvantage is that it is not secure, its size is limited, and it is restricted by the browser security settings. Cookie is actually a small file. These files must be used to store information. Where are these small files? In this case, if the server creates a non-persistent cookie, these files are stored in the browser's memory. Once the browser is completely closed, the cookie becomes invalid; if the server creates persistent cookies, these files are stored in: C: \ your USERNAME \ Default \ appdata \ roaming \ Microsoft \ Windows \ cookies, directly copy the correct path to the address bar of the resource manager and press enter to access the address bar. Do not manually search for the path. The directory cannot be found (even if a hidden file is displayed, the cookies folder cannot be found ), these cookie files are stored here in the form of text files, so the security can be imagined.InProgramIf we set the cookie validity period, the cookie is a persistent cookie. If not set, the default value is non-persistent cookie.

Note: When we access a page, if there is a corresponding cookie, the browser will automatically pass it to the server, and the cookie returned by the server to the client will also be processed by the browser, we don't have to worry about it;Cookie can be overwritten. If a cookie with the same name is repeatedly written, the previous cookie will be overwritten.

The followingCodeShow:"How does the server read the client's cookie?","The server writes non-persistent cookies to the client.","The server writes a persistent cookie to the client.","Differences between persistent and non-persistent cookies","Forcibly invalidates valid persistent cookies". With the above explanation, coupled with detailed comments in the code, I believe the reader will see it at a glance. Copy the following code directly to vs to run properly.

Web Front-end HTML code:

<Body> <Form ID = "frmup" runat = "server" method = "Post"> <div> store \ read cookies: <asp: textbox id = "txtusername" runat = "server"> </ASP: textbox> <asp: button id = "btnsavetemp" runat = "server" text = "writing non-persistent cookies" onclick = "btnsavetemp_click"/> <asp: button id = "btnsavekeep" runat = "server" text = "Write persistence cookies" onclick = "btnsavekeep_click"/> <asp: button id = "btnchangecookie" runat = "server" text = "forced persistence cookie expiration" onclick = "btnchangecookie_click"/> <asp: button id = "btnread" runat = "server" text = "read" onclick = "btnread_click"/> </div> </form> </body>

Web Background C # code:

// Page loading processing protected void page_load (Object sender, eventargs e) {// determine whether cookies exist during loading. If cookies exist, read httpcookie login = request. cookies ["login"]; // obtain the cookie object if (LOGIN! = NULL) {txtusername. TEXT = login ["username"]; // read the attribute value} else {txtusername. TEXT = "invalid cookies" ;}}// the server writes a non-persistent cookie protected void btnsavetemp_click (Object sender, eventargs E) to the client) {httpcookie tempcookie = new httpcookie ("login"); // create an httpcookie instance. The cookie name is login and the instance is only a container, the actual usage is the cookie name tempcookie ["username"] = ""; // Add a username attribute to login and assign a value to response. cookies. add (tempcook IE); // return the cookie object to the client} // The server writes the persistent cookie protected void btnsavekeep_click (Object sender, eventargs E) to the client) {httpcookie keepcookie = new httpcookie ("login"); // create an httpcookie instance. The cookie name is login and the instance is only a container, the actual usage is the cookie name keepcookie ["username"] = ""; // Add a username attribute to login and assign a value to keepcookie. expires = datetime. now. adddays (2); // set the cookie validity period to two days response. cookies. add (keepcookie); // pair cookies Like returned to the client} // forcibly invalidates valid persistent cookies protected void btnchangecookie_click (Object sender, eventargs e) {httpcookie changecookie = new httpcookie ("login "); // If the cookie name is login becomes invalid, the cookie name of the httpcookie object is written as login, which is equivalent to overwriting the old login with the new login, cookie can overwrite changecookie ["username"] = ""; // If the attribute value is still set and cannot be read, it indicates that the cookie has expired changecookie. expires = datetime. now. adddays (-1); // set the cookie validity period to an invalid time to invalidate the cookie response. coo Kies. add (changecookie);} // The server reads the client's cookie protected void btnread_click (Object sender, eventargs e) {httpcookie login = request. cookies ["login"]; // obtain the cookie object if (LOGIN! = NULL) {txtusername. Text = login ["username"]; // read the attribute value in login} else {txtusername. Text = "invalid cookies ";}}



Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.