Session and cookie usage
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