let's introduce a simpler way to use:
Assign a value to a cookie
Copy Code code as follows:
response.cookies["UID"]. Value = "20";
response.cookies["Logincode"]. Value = "58469";
The above is the two cookie variable assigned two different biography, then in the use of cookies, how do we control the validity of cookies, look at the following:
Copy Code code as follows:
response.cookies["UID"]. Expires = DateTime.Now.AddDays (1);
response.cookies["Logincode"]. Expires = DateTime.Now.AddDays (1);
This is the 1-day period of validity for two variables and, of course, a few hours, as follows:
Copy Code code as follows:
response.cookies["Logincode"]. Expires =datetime.now.addhours (1);
This is valid for 1 hours, and the same method can be used as an analogy for a few minutes, a few seconds, and so on.
So how do we get the value of these cookies? It should be noted here that when cookies are not assigned, you will be called to make an error, so we generally before the call will determine whether this cookie exists, methods:
Copy Code code as follows:
if (request.cookies["UID"]!= null)
{
String ruid = request.cookies["UID"]. Value;
}
unique assignment methods in. NET:
Copy Code code as follows:
System.Web.HttpCookie user_cookies_id=new HttpCookie ("UID");
user_cookies_id. Value= "20";
user_cookies_id. Expires=datetime.now.adddays (1);
Response.appendcookie (user_cookies_id);
--------------------------------
HttpCookie acookie=new HttpCookie ("Lastvisitcounter");
Acookie.value=lastvisitcounter. ToString ();
Acookie. Expires=datetime.maxvalue;
RESPONSE.COOKIE.ADD (Acookie);
if (request.cookies["Lastvisitcounter"]==null)
{
textbox1.text= "1";
}
Else
{
HttpCookie acookie=request. cookies["Lastvisitcounter"];
Textbox1.text=server. Htmiencode (Acookie.value);
}
use of session
Session ("username") = "AA" Write
Username=session ("username") read
Session refers to a user's access to a site over a period of time.
The session object corresponds to the HttpSessionState class in. NET, which represents the conversation state, and can hold information related to the current user's session.
The session object is used to store information that is required by a specific user conversation from the beginning of a user's access to a particular ASPX page, until the user leaves. The session object's variables are not purged when the user switches the application's page.
For a Web application, the content of the Application object accessed by all users is exactly the same, while the contents of the session object that are accessed by different user sessions vary.
Session can save variables that can only be used by one user, that is, each Web browser has its own session object variable, that is, the session object has uniqueness.
(1) Add a new item to the session state
The syntax format is:
Session ("key name") = Value or Session.add ("Key Name", value)
(2) Get the value in session state by name
The syntax format is: variable = session ("Key name") or variable = session.item ("Key Name")
(3) Delete items in the session-state collection
The syntax format is: Session.remove ("Key Name")
(4) Clear all values in session state
The syntax format is: Session.removeall () or session.clear ()
(5) Cancel the current session
The syntax format is: Session.Abandon ()
(6) Set the time-out period for session state, in minutes.
The syntax format is: session.timeout = numeric
2 events in the Global.asax file are applied to the session object
Description of event name
Session_Start fires when the session is started
Session_End is fired at the end of the session
Session and cookies in. NET
The session, also known as conversation State, is the most commonly used state in the web system to maintain some information related to the current browser instance. HTTP is a stateless protocol. This means that the WEB server will process each HTTP request for a page as a separate request. The server does not retain any information about the value of the variable used during the previous request. asp.net session state identifies a request from the same browser from a limited time range as a session, and when each user first connects to the WWW server, he establishes a conversation with the server, and the server automatically assigns a sessionid to it. The unique identity used to identify this user.
For example, we can put the user name of the logged-in user in the session, so that we can judge a key in the session to determine whether the user is logged in, if the user name is how much.
Cookies
A cookie is a small piece of textual information that accompanies a user's request and is passed between the Web server and the browser. Each time the user visits the site,
Web applications can read the information that the cookie contains.
Suppose that when a user requests a page from your site, your application is not only the page that returns the request. Also returns an inclusive date
And the time of the cookie. The user's browser also obtains a cookie while getting the page. And cookies are stored in the client's cookie folder.
Specifies the name of the cookie and the value to save. You can create multiple, but each cookie must have a unique name so that it is recognized at a later time when the value is taken. (Cookies are stored by name, and to create two cookies with the same name, the last cookie will overwrite the previous one.) You can also specify an expiration date and time for the cookie. Cookies are written to the user's hard disk and may then remain on the disk. Therefore, specify the expiration date and time of the cookie. The next time the user visits the site, the browser will check all the cookies on your site, and if a cookie has expired, the browser will not send the cookie along with the page request to the server, but delete.
the difference between cookies and session:
1, the cookie data stored in the customer's browser, session data on the server.
2, cookies are not very safe, others can analyze stored in the local cookies and cookie spoofing
Consider that security should use session
3, session will be stored in a certain period of time on the server. When the visit increases, it will take up the performance of your server
Consider the need to use cookies to mitigate server performance
4, a single cookie in the client limit is 3K, that is, a site in the client store cookies can not be 3 K.