Asp.net cookie operations, writes, reads, and operations

Source: Internet
Author: User

Write: CopyCode The Code is as follows: httpcookie cookie = new httpcookie ("id_admin _");
Cookie. value = model. id_admin _. tostring ();
// Cookie. Domain = ".sosuo8.com ";
Httpcontext. Current. response. Cookies. Add (cookie );

Cookie = new httpcookie ("name_admin _");
// It may be a Chinese character and must be encoded
Cookie. value = httputility. urlencode (model. name_admin _);
// Cookie. Domain = ".sosuo8.com ";
Httpcontext. Current. response. Cookies. Add (cookie );

Cookie = new httpcookie ("guid ");
Cookie. value = guid. newguid (). tostring ();
// Cookie. Domain = ".sosuo8.com ";
Httpcontext. Current. response. Cookies. Add (cookie );

Read:Copy codeThe Code is as follows: httpcontext. Current. Request. Cookies ["guid"]. Value

Asp.net clear cookie clear a single Copy codeThe Code is as follows: Response. Cookies ["admin"]. expires = datetime. Now. adddays (-1 );

Asp.net clear cookie clear all
Request. Cookies. Clear () is not used to delete cookies.

deleting a cookie (physically removing a cookie from a user's hard disk) is a form of cookie modification.
the cookie cannot be directly removed because it is on the user's computer.
however, the browser can delete cookies for you.
This technique creates a new cookie with the same name as the cookie to be deleted.
set the cookie expiration date to a date earlier than the current date.
when the browser checks the cookie expiration date, the browser discards the expired cookie.
the following code example demonstrates how to delete all available cookies in the Program of an Application: copy Code the code is as follows: httpcookie acookie;
string cookiename;
int Limit = request. cookies. count;
for (INT I = 0; I {< br> cookiename = request. cookies [I]. name;
acookie = new httpcookie (cookiename);
acookie. expires = datetime. now. adddays (-1);
response. cookies. add (acookie);
}

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.