Cookie lost when vs re-run

Source: Internet
Author: User
Tags http cookie

I wrote a demo about HTTP cookie today and found that the cookie value will be lost as long as vs is re-run. The Code is as follows:
 
Protected void page_load (Object sender, eventargs e) {httpcookie _ userinfocookies = request. Cookies ["username"]; string city; If (_ userinfocookies! = NULL) {city = _ userinfocookies ["city"]; session ["location"] = city; response. redirect ("berava. aspx ") ;}} protected void moveto_click (Object sender, eventargs e) {session [" location "] = worlddrdolist. selecteditem. value; If (checkbox1.checked) {httpcookie _ userinfocookies = new httpcookie ("username"); _ userinfocookies ["city"] = worlddrdolist. selectedvalue. tostring (); response. cookies. add (_ userinfocookies);} response. redirect ("berava. aspx ");}
The last result is because the cookie expiration time is not set (expire). The default cookie is only valid for the current session, so vs is a new session when it is re-run, in this case, the cookie has expired. You only need to set an expiration time for the cookie, as shown below (the effective time is one day ):
 
_ Userinfocookies. expires = datetime. Now. adddays (1 );


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.