Webform (6) -- logon status persistence (built-in Cookies) and webformcookies

Source: Internet
Author: User

Webform (6) -- logon status persistence (built-in Cookies) and webformcookies

A user accesses a website through a browser. Due to the http feature, the Web server does not know which user is visiting. However, some websites want to know some information about the visitor, for example, whether a visitor visits the website for the first time, whether there is any unfinished work during the last visit, and whether it is convenient for the visitor to continue his work this time. When you access a website through a browser, you can jump between the webpage of the website. When you switch from the first webpage to the second webpage, all the variables and objects created in the first webpage will not exist. Sometimes you want to establish contacts on these accessed webpages. For example, in an online store, visitors may select different products from different webpages. How can they record the products selected by the visitor, that is, how to implement the shopping basket. The Cookie object can solve the above problems.

I. cookies

Summary:

A piece of text saved on the hard drive of your computer

The http protocol includes a browser that allows websites to temporarily store data on users' computers in the form of Cookies.

If no storage time is set, that is, when cookies are session:

1. If you do not refresh the page again within 20 minutes, the cookies will be automatically deleted.

2. The current access connection is interrupted. If the browser is closed, the cookies will be automatically deleted.

If you set the storage time, the cookies will be automatically deleted after the time.

Of course, the key to the preservation of cookies is that users can manually identify cookies.

Purpose: Maintain the user's login status

Usage:

1. Get account: Response. Cookies ["user"]. Value = TextBox1.Text;

2. Set the expiration time for Logon persistence for this account: Response. Cookies ["user"]. Expires = DateTime. Now. AddDays (7 );

3. Clear cookies: Response. Cookies ["user"]. Expires = DateTime. Now. AddDays (-5); if the value is negative, it indicates that it has expired for several days.

 

 

Ii. log on to the instance to maintain the status

Create a Login logon page:

Front-end code:

<Form id = "form1" runat = "server"> Username: <asp: TextBox ID = "TextBox1" runat = "server"> </asp: textBox> <br/> password: <asp: TextBox ID = "TextBox2" runat = "server"> </asp: TextBox> <br/> <asp: checkBox ID = "CheckBox1" runat = "server"/> <label for = "CheckBox1"> remember the logon status for 7 days </label> <br/> <asp: button ID = "Button1" runat = "server" Text = "login"/> </form>

Background code:

Protected void Page_Load (object sender, EventArgs e) {Button1.Click + = button#click;} void button#click (object sender, EventArgs e) {// value, database Query bool OK = new UsersDA (). select (TextBox1.Text, TextBox2.Text); if (OK) {// if data is found, you can log on to Response successfully. cookies ["user"]. value = TextBox1.Text; if (CheckBox1.Checked) {// set the storage time Response for cookies. cookies ["user"]. expires = DateTime. now. addDays (7);} Response. redirect ("Main. aspx ");}

Main background code on the Main interface:

// Determine whether or not cookies are empty. if (Request. cookies ["user"]! = Null) {Users u = new UsersDA (). select (Request. cookies ["user"]. value); Labdl. text = u. nickName; Literal1.Text = ", welcome! ";}// Null, jump to the logon page else {Response. Redirect (" Login. aspx ");}


If you have logged in, you can log out:

The Main interface exits the logon front-end code:

<Asp: Button ID = "Btntc" runat = "server" Text = "log out"/>

Background code (clear cookies ):

Void Btntc_Click (object sender, EventArgs e) {// 1 clear cookies Response. cookies ["user"]. expires = DateTime. now. addDays (-5); // 2 refresh the page/jump to the login page Response. redirect ("Login. aspx ");}

 

 

 

 

 

 

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.