Troubleshooting of cookies in Asp.net applications

Source: Internet
Author: User

1. Cookie confusion may occur in applications. The main problem is removecookie. solution:

 

/// <Summary>

/// Remove the cookie and clear both the request and response collections.

/// Code by kentli

/// </Summary>

/// <Param name = "cookiename"> cookie name </param>

Public void removecookie (string cookiename)

{

Httpcookie cookie = httpcontext. Current. Request. Cookies [cookiename];

If (cookie! = NULL)

{

// Set the expiration time to expire immediately.

Cookie. expires = datetime. now;

Httpcontext. Current. Request. Cookies. Remove (cookiename );

}

 

Cookie = httpcontext. Current. response. Cookies [cookiename];

If (cookie! = NULL)

{

Cookie. expires = datetime. now;

Httpcontext. Current. response. Cookies. Remove (cookiename );

}

}

 

Public void appendcook (string cookies, string cookiesvalue, int days)

{

Httpcookie usercookie = new httpcookie (cookies, cookiesvalue );

Usercookie. expires = datetime. Now. adddays (days );

Httpcontext. Current. response. Cookies. Add (usercookie );

}

 

2

The Chinese cookie is normal in Windows 2000 and sometimes abnormal in Windows 2003 (the probability is very small, but it does exist). Use:

Server. urlencode ();

Server. urldecode ();

Encoding and decoding.

 

 

 

Cookie ["mycookie"] = server. urlencode ("Chinese ")

Response. Write (server. urldecode (request. Cookies ("mycookie"). Value ()))

 

 

3. Completely Delete cookies

Protected void deletecookie (string cookiename)
{
If (request. Cookies [cookiename]! = NULL)
{
Httpcookie cookie = new httpcookie (cookiename );
Cookie. expires = datetime. Now. adddays (-1 );
Response. appendcookie (cookie); // This sentence is very important
}
}

 


Http://support.microsoft.com/default.aspx? SCID = KB; en-US; 313282

 

More information about cookies

 

Httpcontext. Current. response. Cookies

Httpcontext. Current. Request. Cookies

 

The cookie set that needs to be written back to the user client and read from the user client.

When you modify a cookie, to ensure that the same Asp.net page can access the modified cookie, you must modify the cookies in both sets.

 


Http://blog.joycode.com/ghj/archive/ 2003/11/06 /5647. aspx

Http://west-wind.com/weblog/posts/1006.aspx

Http://www.derkeiler.com/Newsgroups/microsoft.public.inetserver.iis.security/2004-03/0147.html

 

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.