asp.net cookies operation, write, read and operate _ Practical tips

Source: Internet
Author: User
Tags datetime
Write:
Copy Code code 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_");
May be Chinese characters, 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 Code code as follows:

Httpcontext.current.request.cookies["GUID"]. Value

asp.net empty cookies empty individual
Copy Code code as follows:

response.cookies["Admin"]. Expires = DateTime.Now.AddDays (-1);

asp.net empty cookies empty all
Request.Cookies.Clear () This method does not delete cookies

deleting cookies (that is, physically removing cookies from the user's hard disk) is a form of modifying cookies.
Because the Cookie is on the user's computer, it cannot be removed directly.
However, you can have your browser remove cookies for you.
The technique is to create a new cookie with the same name as the cookie you want to delete.
and set the expiration date of the Cookie to a date earlier than the current date.
When the browser checks the expiration date of the cookie, the browser discards the cookie that is now expired.
The following code example demonstrates a way to delete all available cookies in an application:
Copy Code code as follows:

HttpCookie Acookie;
String CookieName;
int limit = Request.Cookies.Count;
for (int i = 0; i < limit; i++)
{
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.