Asp.net cookie value acquisition delete display code

Source: Internet
Author: User

Asp tutorial. net cookie value acquisition delete display code
Cookie is a short text message that is transmitted between the web server and the browser along with user requests and pages.

Delivery. Cookie contains information that can be read by web applications every time a user accesses the site.

For example, if the application sends a page to the user when the user requests a page in the site

And a cookie containing the date and time. The user's browser also obtains

This cookie is stored in a folder on the user's hard disk.

 

Set cookie

<% @ Page language = "vb" %>
<Html>
<Head>
<Title> setting a cookie in asp.net tutorial </title>
<Script runat = "server">
Sub page_load ()
Dim mycookie as new httpcookie ("loggedin ")
Mycookie. value = "true"
Mycookie. expires = datetime. now. addminutes (30)
Response. cookies. add (mycookie)
End sub
</Script>
</Head>
<Body>
<Asp: label id = "message" runat = "server"/>
</Body>
</Html>

Show all cookies

<% @ Page language = "c #" %>
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en"

Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Script runat = "server">

Void page_load ()
    {

Arraylist colcookies = new arraylist ();
For (int I = 0; I <request. cookies. count; I ++)
Colcookies. add (request. cookies [I]);

Grdcookies. datasource = colcookies;
Grdcookies. databind ();
    }
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> get all cookies </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
   
<Asp: gridview
Id = "grdcookies"
Runat = "server"/>
       
</Div>
</Form>
</Body>
</Html>

Delete all cookie codes

<% @ Page language = "c #" %>
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en"

Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Script runat = "server">
   
Void page_load ()
    {
String [] cookies = request. cookies. allkeys;
Foreach (string cookie in cookies)
        {
Bulletedlist1.items. add ("deleting" + cookie );
Response. cookies [cookie]. expires = datetime. now. adddays (-1 );
        }
    }
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> delete all cookies </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
   
<H1> delete all cookies
   
<Asp: bulletedlist
Id = "bulletedlist1"
Enableviewstate = "false"
Runat = "server"/>
   
</Div>
</Form>
</Body>
</Html>


If no cookie validity period is set, a cookie will still be created, but it will not be stored in the user's hard

Disk. The cookie is maintained as part of the user's session information. When the user closes the browser

The cookie is discarded. This non-permanent cookie is suitable for storing

Information, or save the disk information that should not be written to the client computer for security reasons. For example

If you are using a public computer and do not want to write cookies to the disk of the computer

You can use non-permanent cookies.

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.