A. Cookies:
Cookies mainly store some insensitive data and can only store string types
Execution process:
(1) First request:
The client requests the server-side response in the form of a request packet, such as a user name.
Server-side to get the data (user name),
If the login succeeds, it is stored on the client with a cookie, and the cookie is created and assigned the following values:
New HttpCookie (" key "," value "); RESPONSE.COOKIES.ADD (cookie);
The cookie object can also be obtained from the request message sent by the browser:
HttpCookie Cookie = context. Request.cookies[kits.id];
(2) Second request:
This will satisfy the requirements, and when the user requests the page again, they can judge the user's cookie,
To pre-populate some data.
The value code for the cookie is as follows:
request.cookies[" key "]. Value.tostring ();
The cookie can be set to expire (set cookie to disk cookie):
Cookies. Expires = DateTime.Now.AddHours (1);
Determine if a cookie exists (determines whether the obtained cookie object is empty)
if (request.cookie[" key "]==null) { }
Second, Session:
To implement the session function, the System.Web.SessionState.IRequiresSessionState tag interface must be implemented
The existence of the session is also to eliminate the user repetitive to enter some data,
The difference is that the session can store some data-sensitive data (Pwd, passwords), which can be of any type,
This can be achieved without login (after the user login once, if you do not exit the browser, the second time can go directly to the homepage),
Execution process:
(1) First time request
The browser requests the server-side response in the form of a request packet,
The server determines whether the user name password is correct,
After right, save the data with the session code as follows:
The session is stored as a key-value pair:
Context. session[" key "]=" value ";
This step is done automatically on the server side, and the server will save the session to the session pool,
The keys (Asp.net_sessionid) of the objects in the session pool are randomly generated,
The value in the session pool is the session key value pair that you just stored, context. session["PWD"],
Returns the Asp.net_sessionid as a cookie, which is stored in a cache on the browser side,
(2) Second request
After the first request, the browser has obtained a asp.net_sessionid,
On the second request, the Asp.net_sessionid is taken to the request header,
This allows the 9th to 11th events of the pipeline event to be performed on an ASPX page, according to the Asp.net_sessionid
Locate the corresponding Session object in the session pool and assign the object to the context object.
The value code for the session is as follows:
context.session[" key "]. ToString ();
The code to clear the session is as follows:
Context.Session.Abandon ();
Determine if session is present (determines whether the session object obtained is empty)
if (context.session[" key "]==null) { }
Third, you can use the combination of cookies and session to some functions
(1) Requirements: After the user logs on a successful page, you can log on to other pages later without having to go through the login page,
If you do not exit the browser, once you exit the browser, the user will need to re-login page
This is done through the session.
After successful login, the name of the user to be found is stored in the session:
Context. session[" key "]=" value ";
Each subsequent page first determines whether the session exists:
if (context.session[" key "]==null) { }
(2) Requirements: To achieve the user's 7-day free login,
This is done through a combination of session and cookie
After successful login, the name of the user to be found is stored in the session:
Context. session[" key "]=" value ";
If the user chooses to log-off, because the cookie cannot store user-sensitive data,
Record the current user's ID with a cookie and set the expiration time to 7 days,
New HttpCookie (" key "," value "= DateTime.Now.AddDays (7 ); RESPONSE.COOKIES.ADD (cookie);
After judging the existence of the session, then determine whether the user ID cookie exists,
If the cookie is present, the ID is taken out and the user name is assigned to the session in the database.
stringid= request.cookies[" key "]. Value.tostring (); then query out the user name, context. session[" key " " user name ";
(3) Requirements: After successful login, display the user name on each page
Through session implementation
The following code is on the ASPX foreground page:
<%= session[" key "]%>
Some summaries of the cookie and session