Ten days to learn ASP.net (10)

Source: Internet
Author: User

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.


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.