Introduction to cookie usage in asp.net-basic application

Source: Internet
Author: User
Tags current time datetime
A. Cookie guide to understand what a cookie is

1. What is Cookie:cookie 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 Web site, a very small text file placed on your hard drive by a Web server can record your user ID, the page you've browsed, or the time you've visited, and the information your site wants you to keep. When you visit the website again through the browser, the browser will automatically send the cookies belonging to the Web site to the server, the server by reading cookies, know your relevant information, you can make the appropriate action. For example, the display welcome your small title, does not need to fill in the account password direct login and so on.
3. Different browsers store cooks locations that are not the same. The information in the cookie file is not secure, so the data in the cookie is best encrypted.
4. The browser saves cookie data in 2 of the form: in the browser's memory, the browser's computer's hard drive.

two. View of Cookies

How cookies are viewed in the hard drive:

three. Code explanation for cookies
1. Write cookies to browser end
Copy Code code as follows:

HttpCookie cookie = new HttpCookie ("id", "234"); Creates an instance of a 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 to id:234 in a cookie file, we can read this data
2. Read the data stored in the cookie
Copy Code code as follows:

HttpCookie cookie = new HttpCookie ("id", "234"); Creates an instance of a 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 page writes out the data is 234, from here we can see that the cookie is not safe. Therefore, it is best not to store important information, if you want to store, you can encrypt it, in the write cookie storage file. And if you write to it indefinitely, it can cause too much junk. So we can add an expiration date to the cookie file.
3.cookie File Expiration settings
Copy Code code as follows:

HttpCookie cookie = new HttpCookie ("id", "234"); Creates an instance of a cookie.
Cookie. Expires = DateTime.Now.AddMonths (5);//Set the expiration time of the cookie, expire after 5 minutes, and automatically clear 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

4.cookie file deletion, destruction
Copy Code code as follows:

HttpCookie cookie = new HttpCookie ("id", "234"); Creates an instance of a cookie.
Cookie. Expires = DateTime.Now.AddMonths (5);//Set the expiration time of the cookie, expire after 5 minutes, and automatically clear 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
Cookie. Expires = DateTime.Now.AddMonths (-5); Cookie was destroyed, and gave him a set of time to go much longer, and he destroyed it on multiple times.

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 log in directly to jump content page)
Copy Code code as follows:

<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 && request.cookies["PassWord"] = = null)
{
if (request.form["userName"]!= null && request.form["pass"]!= null)
{
String userName = request.form["UserName"];
String UserPassword = request.form["Pass"];
if (userName = "admin" && userpassword = = "123")
{
if (request.form["sele1"]!= 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 count back two days
Cookiepassword.expires = new DateTime (2012, 5, 27); Sets the expiration time for the password cookie, which expires May 27, 2012
RESPONSE.COOKIES.ADD (Cookieusername); Enter the Cookieusername file you created into the browser side
RESPONSE.COOKIES.ADD (Cookiepassword);
Response.Redirect ("1.aspx"); Jump to the page you want
}
Else
{
Response.Redirect ("1.aspx"), or even do not remember the password to jump
}
}
}
}
Else
{
Response.Redirect ("1.aspx");//If you remember the password, the second login will go directly to the 1.aspx page
}
}

Five. js cookies to write later, here I have any errors, or you want to guide me, please leave a message. Thank you!
Related Article

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.