Day 10
Objective: To learn how to use SESSION and COOKIE
SESSION and COOKIE are used for variable transmission between webpages and for recording user login information. Using cookies in ASP. NET is a little more troublesome than using cookies in ASP, because we need to declare variables.
First, let's take a look at the SESSION usage, which is basically similar to ASP.
Write a SESSION:
Session ["username"]) = "aa ";
Session ("username") = "aa"
Read a SESSINN:
String username = Session ["username"];
Dim username = Session ("username ")
Let's take a look at the COOKIE writing:
DateTime dt = DateTime. Now; // You must <% @ Import Namespace = "System" %> to obtain the current time.
HttpCookie mycookie = new HttpCookie ("logname"); // declare the new COOKIE variable
Mycookie. Value = "aa"; // Value assignment
Mycookie. Expires = Convert. ToDateTime (dt + TimeSpan. FromDays (1); // set the expiration time to 1 day.
Response. Cookies. Add (mycookie1); // write COOKIE
Dim dt as DateTime
Dt = DataTime. Now
Dim mycookie as HttpCookie
Mycookie = new HttpCookie ("logname ")
Mycookie. Value = "aa"
Mycookie. Expires = Convert. ToDateTime (dt + TimeSpan. FromDays (1 ))
Response. Cookies. Add (mycookie1)
Take a look at the COOKIE reading:
HttpCookie mycookie = Request. Cookies ["username"];
String username = mycookie. Value;
Dim mycookie as HttpCookie
Mycookie = Request. Cookies ["username"]
Dim string = mycookie. Value
Now ASP. NET is over.