C #. NET and VB.net read/write cookies!

Source: Internet
Author: User

Cookie (httpcookie instance) provides a web applicationProgramTo store user-specific information. For example, when a user accesses your site, you can use cookies to store user preferences or other information. When the user visits your website again, the application can retrieve the previously stored information.

C #. Net Section

Cookie creation method (1)
Response. Cookies ["username"]. value = "admin ";
Response. Cookies ["username"]. expires = datetime. Now. adddays (1 );
// If no expiration time is set, the cookie information will not be written to the user's hard disk, and will be discarded if the browser is closed.

Cookie creation method (2)
Httpcookie acookie = new httpcookie ("lastvisit"); // last access time
Acookie. value = datetime. Now. tostring ();
Acookie. expires = datetime. Now. adddays (1 );
Response. Cookies. Add (acookie );

Cookie Access Method (1)
If (request. Cookies ["username"]! = NULL)
Label1.text = server. htmlencode (request. Cookies ["username"]. Value); Cookie Access Method (2)
If (request. Cookies ["username"]! = NULL)
{
Httpcookie acookie = request. Cookies ["username"];
Label1.text = server. htmlencode (acookie. value );
}

Method for creating multi-value cookies (1)
Response. Cookies ["userinfo"] ["username"] = "admin ";
Response. Cookies ["userinfo"] ["lastvisit"] = datetime. Now. tostring ();
Response. Cookies ["userinfo"]. expires = datetime. Now. adddays (1 );

Method for creating multi-value cookies (2)
Httpcookie acookie = new httpcookie ("userinfo ");
Acookie. Values ["username"] = "admin ";
Acookie. Values ["lastvisit"] = datetime. Now. tostring ();
Acookie. expires = datetime. Now. adddays (1 );
Response. Cookies. Add (acookie );

Read multi-value cookies

Httpcookie acookie = request. Cookies ["userinfo"];
String username = acookie. Values ["username"];
String lastvisit = acookie. Values ["lastvisit"];

VB.net

Cookie creation method (1)
Response. Cookies ("username"). value = "admin"
Response. Cookies ("username"). expires = datetime. Now. adddays (1)
'If no expiration time is set, the cookie information will not be written to the user's hard disk and will be discarded if the browser is closed.


Cookie creation method (2)
Dim acookie as httpcookie = new httpcookie ("lastvisit") 'last access time
Acookie. value = datetime. Now. tostring ()
Acookie. expires = datetime. Now. adddays (1)
Response. Cookies. Add (acookie)


Cookie Access Method (1)
If not request. Cookies ("username") is nothing then
Label1.text = server. htmlencode (request. Cookies ("username"). Value) method for accessing cookies (2)
End if
If not request. Cookies ("username") is nothing then
Dim acookie as httpcookie = request. Cookies ("username ")
Label1.text = server. htmlencode (acookie. value)
End if


Method for creating multi-value cookies (1)
Response. Cookies ("userinfo") ("username") = "admin"
Response. Cookies ("userinfo") ("lastvisit") = datetime. Now. tostring ()
Response. Cookies ("userinfo"). expires = datetime. Now. adddays (1)


Method for creating multi-value cookies (2)
Dim acookie as httpcookie = new httpcookie ("userinfo ")
Acookie. Values ("username") = "admin"
Acookie. Values ("lastvisit") = datetime. Now. tostring ()
Acookie. expires = datetime. Now. adddays (1)
Response. Cookies. Add (acookie)


Read multi-value cookies


Dim acookie as httpcookie = request. Cookies ("userinfo ")
Dim username as string = acookie. Values ("username ")
Dim lastvisit as string = acookie. Values ("lastvisit ")


Modify and delete cookies

You cannot directly modify or delete a cookie. You can create only one new cookie and send it to the client to modify or delete the cookie.

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.