Question about cookies in ASP.NET

xiaoxiao2021-03-06  86

Problem for cookies in ASP.NET applications:

1. Sometimes the problem of cookie confusion in the application, the main problem is on RemoveCookie, a solution:

///

/// Remove cookie, clean all the two collections of Request and Response

/// Code by Kentli

///

/// cookie name

Public void RemoveCookie (String Cookiename)

{

Httpcookie cookie = httpContext.current.request.cookies [cookiename];

IF (cookie! = null)

{

//

Expired time is set 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.addays (days);

HttpContext.current.Response.cookies.add (usercookie);

}

2, Chinese cookie's problem, normal in Windows 2000, sometimes not normal in Windows 2003 (small in case, but indeed), adopting:

Server.urlencode ();

Server.urldecode ();

Coding and decoding.

Cookie ["MyCookie"] = Server.urlencode ("Chinese")

Response.write (Request.Cookies ("MyCookie") .value ())) http://support.microsoft.com/default.aspx?scid=kb;n-us; 313282

More cookies related knowledge

HttpContext.current.response.cookies

HttpContext.current.Request.cookies

They are a Cookie collection that needs to be written back to the user client and read from the user client.

When modifying the cookie, in order to ensure that the same ASP.NET page can access the modified cookie, you must also modify the cookies in these two collections. Http://blog.joycode.com/ghj/archive/

2003/11/06

/5647.aspx

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

转载请注明原文地址:https://www.9cbs.com/read-97855.html

New Post(0)