Usage of session and cookie objects in WebForm

Source: Internet
Author: User

(a) Session

Used as cookies to store user data

1. Session and Cookie Comparison

The same point: each computer access to the server, will be a separate set of sessions or Cookies,key values are the same, but the content is not the Same.

Different points: The session is saved in the server memory, the cookie is stored on the User's hard disk

The session is not persistent, and it is stored for 20 minutes without any action on the page, and the cookie can be long-lasting or non-durable, depending on the user

2. Session Focus

Do not misuse the session, do not use, misuse may cause server content overflow, do not cause waste of resources, because in-memory data extraction and interaction is the fastest

3. Usage

Assignment:session["key"] = value;

Value:String a = session["key"];

Clear: session["key"]=null;

note:1and don't save a lot of data in the Session.2, after the temporary session to take the useless remember =null to empty it//Assign Valuestrings =textbox1.text; session["AA"] =s;//Take valueif(session["AA"]!=NULL) Label1.Text=session["AA"].tostring ();

Two Cookies

Simple summary:

A piece of text saved on the hard drive of the User's computer

The HTTP protocol includes a browser that allows the site to temporarily store data in the form of cookies on the User's computer.

If the save time is not set, i.e. session Cookies:

1, If you do not refresh the page in 20 minutes, then this cookie will be automatically deleted

2, The current access connection is interrupted, such as closing the browser, then the cookies will be automatically deleted

If the save time is set, the cookie will be deleted automatically after the time

Of course, the cookies are saved or not, the key in the user, the user can manually clear the Cookie.

Function: Keep the User's login status

Usage:

1. Get account Number: response.cookies["user". Value = textbox1.text;

2, set the login to keep the expiration time: response.cookies["user". Expires = DateTime.Now.AddDays (7);

3. Clear cookies:response.cookies["user"]. Expires = DateTime.Now.AddDays (-5); Just leave the value negative to indicate that it has expired for several days

//Assign Valuestrings =textbox1.text; response.cookies["username"]. Value =s;if(checkbox1.checked) {response.cookies["username"]. Expires =datetime.now.adddays (3);//set the expiration time of a cookie            }//Take valueif(request.cookies["username"] !=NULL) Label1.Text= request.cookies["username"]. Value; Cookies

Server.Transfer ("default2.aspx"); Use this method to jump to a page without changing the address displayed in the Address bar (for example, If you must address the location bar but do not want the user to see the Variable)

To set up a logon login page:

Front code:

<form id= "form1" runat= "server" >    user name: <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 login status 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 + = button1_click;    }    void button1_click (object sender, EventArgs e)    {        //fetch value, database query        bool OK = new USERSDA (). Select (textbox1.text, textbox2.text);        If (ok)        {            ///Jocha to data, you can login successfully            response.cookies["user"]. Value = textbox1.text;            If (checkbox1.checked)            {                //the cookie is set to save time                response.cookies["user"]. Expires = DateTime.Now.AddDays (7);            }            Response.Redirect ("main.aspx");        }

Main interface main background code:

Determine if the cookie is empty, is not empty, and is based on the value of the cookie to inquire        if (request.cookies["user"]! = Null)        {            Users u = new USERSDA (). Select (request.cookies["user"). Value);            Labdl.text = u.nickname;            Literal1.text = "welcome to you!" ";        }        empty, Jump to login page        else        {            Response.Redirect ("login.aspx");        }


There is a login to sign Out:

Main interface Exit Login foreground code:

<asp:button id= "btntc" runat= "server" text= "exit login"/>

Background Code (clear cookies):

void Btntc_click (object sender, EventArgs e)    {        //1 clears        the cookie response.cookies["user"]. Expires = DateTime.Now.AddDays ( -5);        2 Refresh Page/jump to landing page        Response.Redirect ("login.aspx");    }

Usage of session and cookie objects in WebForm

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.