asp.net cookies need to pay attention to the point _ practical skills

Source: Internet
Author: User
Tags send cookies
As cookies are easy to set up and easy to use, there is no expires problem with cookies in the process of directly reading and writing cookies.
When a cookie instance is obtained from request, the cookie is sent to the client again if the expires is not reset, and the expires will become expired on the closed page.
For example:
Send cookies first in a page
HttpCookie _currentcookie;
_currentcookie = new HttpCookie ("test");
_currentcookie.expires = DateTime.Now.AddYears (1);
Response.appendcookie (_currentcookie);
And then another page to receive it
HttpCookie _currentcookie;
_currentcookie = request.cookies["Test"];
At this point, we can get and display the expires expiration time.
Response.Write (_currentcookie.expires.tostring ());
According to common sense, we did not change this cookie, get the intact after the send to the browser, should not have any changes.
But there is actually a problem.
_currentcookie["Test"]. Expires becomes expired after the 0001-1-1 closing of the page.
Through the above experiments, we found that we could not actually get the expires property of cookies, so When you use Response.appendcookies, if the instance does not reset expires, then expires is sent with the default setting (page shutdown expires), so each time you send cookies, you must reset them if you want to have an expiration date.
This problem I encountered while encapsulating cookies, although used to be very often used cookies, but this time because I want to encapsulate a separate class, so in the process of streamlining the code encountered such a problem!
It seems that no matter how familiar things, it will always have a strange side, need to learn a lot!
Of course, this is in. In NET2.0 C #, it seems that 1.1 can get the expires attribute of the cookie from the request.
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.