Cookies in ASP.

Source: Internet
Author: User

I. Cookie guidance to understand what a cookie is

1. What is Cookie:cookie is a hard disk or memory that allows a Web server to store a small amount of data (around 4KB) to the client. And read a technique that can be taken out.

2. When you browse a website, a very small text file that is placed on your hard drive by the Web server, it can record your user ID, the page visited or the time of stay and other sites want you to save information. When you visit the website again through a browser, the browser will automatically send the cookie that belongs to the website to the server, the server can make the corresponding action by reading the cookie and knowing your relevant information. For example, show Welcome to your small title, do not fill in the account password directly login and so on.
3. The cooks location of different browser storage is not the same. The information in the cookie file is not secure, so the data inside the cookie is best encrypted.
4. The browser saves the cookie data in 2 forms: The browser's memory, the browser's computer hard drive.

two. Viewing of Cookies

How the cookie exists in the hard disk view method:

Iii. code Interpretation of Cookies

1. Write cookies to the browser side

HttpCookie cookie = new HttpCookie ("id", "234"); Create an instance of the cookie. RESPONSE.COOKIES.ADD (cookie);//Enter the cookie file you created into the browser side

explain: This is equivalent to writing a key value pair in a cookie file for id:234, we can read this data

2. Read the data stored in the cookie

HttpCookie cookie = new HttpCookie ("id", "234"); Create an instance of the cookie. RESPONSE.COOKIES.ADD (cookie);//Enter the cookie file you created into the browser-side Response.Write (request.cookies["id"). Value); Read the value stored in the cookie file

explain: The data written on the page is 234, and from here we can see the security of the cookie. So it is best not to store important information when you use it, and if you want to store it, you can encrypt it and write it in the cookie store file. There are also too many garbage files if there is no unrestricted write to them. So we can add an expiration date to the cookie file.

The expiration date setting of the 3.cookie file

HttpCookie cookie = new HttpCookie ("id", "234"); Create an instance of the cookie.            cookies. Expires = DateTime.Now.AddMonths (5);//Set the expiration time of the cookie, expires after 5 minutes, automatically clears the file            Response.Cookies.Add (cookie);// Enter the cookie file you created into the browser-side            Response.Write (request.cookies["id"). Value); Read the value stored in the cookie file

Deletion and destruction of 4.cookie files

            HttpCookie cookie = new HttpCookie ("id", "234"); Create an instance of the cookie.            cookies. Expires = DateTime.Now.AddMonths (5);//Set the expiration time of the cookie, expires after 5 minutes, automatically clears the file            Response.Cookies.Add (cookie);// Enter the cookie file you created into the browser-side            Response.Write (request.cookies["id"). Value); Reads the value cookie stored in the cookie file            . Expires = DateTime.Now.AddMonths (-5); The destruction of the cookie, gave him the time to set an extra go, and he destroyed it twice.

Four. A small example of a cookie, remember me. (If you choose to remember me when you log in, the next time you do not need to login directly to the content page)

<body>    <form id= "Form1" method= "Post" action= "rembpage.aspx" >    <div>        account number: <input Type= "text" name= "UserName"/><br/>        Password: <input type= "password" name= "pass"/><br/>        Remember me: <input type= "checkbox" value= "REM" name= "Sele1"/><br/> <input        type= "Submit" value= "Login"/>     </div>    </form></body>

 protected void Page_Load (object sender, EventArgs e) {if (request.cookies["userName"] = = NULL &&am P request.cookies["PassWord"] = = NULL)//judgment master exists cookie, if present indicates last selected remember me {if (request.form["UserName "]! = null && request.form[" Pass "]! = null) {String userName = request.form[" us                    Ername "];                    String UserPassword = request.form["Pass"]; if (UserName = = "Admin" && userpassword = = "123") {if (request.form["se Le1 "]! = null) {HttpCookie cookieusername = new HttpCookie (" UserName ",  UserName);                            Create an Account cookie instance HttpCookie Cookiepassword = new HttpCookie ("PassWord", UserPassword);   Cookieusername.expires = DateTime.Now.AddDays (2); Set the expiration time of the account cookie, the current time is two days cookiepassword.expires = new DatetiMe (2012, 5, 27);  Set the expiration time of the password cookie, which expires May 27, 2012 RESPONSE.COOKIES.ADD (Cookieusername);                            Enter the created Cookieusername file into the browser-side RESPONSE.COOKIES.ADD (Cookiepassword); Response.Redirect ("1.aspx"); Jump to the page you want} else {Respo Nse. Redirect ("1.aspx");//Even if you do not remember the password to jump}}} El SE {Response.Redirect ("1.aspx");//If you remember the password, the second login will go directly to the 1.aspx page}}

Cookies in ASP.

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.