The cookie application in ASP. NET, which corresponds to multiple values

Source: Internet
Author: User

Set cookie

Protected void page_load (Object sender, eventargs e) <br/>{< br/> // declare a reference to an httpcookie <br/> httpcookie cookie = NULL; <br/> // count the number of visits <br/> int COUNT = 0; <br/> // determines whether the access request is a page addition. <br/> If (! Page. ispostback) <br/>{< br/> // read the "userinfo" in the session range, because we store the userinfo type, therefore, we can directly convert this type to <br/> userinfo = session ["userinfo"] As userinfo; <br/> If (userinfo = NULL) <br/> {<br/> response. redirect ("login. aspx "); <br/>}< br/> else if (userinfo. username! = "Jrain" | userinfo. Password! = "1234") <br/>{< br/> response. redirect ("login. aspx "); <br/>}< br/> else <br/> {<br/> // read the cookie named" login "from the client, <br/> cookie = request. cookies ["login"]; <br/> // If the cookie does not exist, the cookie is written for the first time. <br/> If (cookie = NULL) <br/>{< br/> // if the client does not have this cookie, We need to instantiate the cookie named login first, <br/> cookie = new httpcookie ("login"); <br/> // Add the number of visits <br/> count ++; </P> <p> // note that only values of the string type can be added to a cookie </P> <p> // set the cookie value. If there are multiple values We use cookies. values ["loginname"] method <br/> cookie. values ["loginname"] = userinfo. username; <br/> cookie. values ["datetime"] = datetime. now. tostring (); <br/> cookie. values ["count"] = count. tostring (); </P> <p>/* set the cookie expiration time. Note: <br/> to save data to the hard disk, you must set the cookie expiration time. Otherwise, the cookie will only be stored in the memory. <br/> expires attribute, which sets the cookie expiration date and time. If the cookie validity period is not set (the default setting), <br/> You can also create a cookie, but it will not be saved to the user's hard disk, but will become part of the user's session information, <br/> when the browser is closed or the session times out, the cookie will disappear. This cookie is called a non-permanent cookie. <Br/> the cookie for storing sessionid is such a cookie, which is not stored on the hard disk and only exists in the memory. */<Br/> // cookie. the expires value is an enumeration of datetime <br/> cookie. expires = datetime. now. adddays (30); <br/> // write the cookie to the client <br/> response. cookies. add (cookie ); <br/>}< br/> else // when the client has a cookie currently searched <br/> {<br/> // number of visits read <br/> COUNT = int. parse (cookie. values ["count"]. tostring (); <br/> // times plus <br/> count ++; <br/> // read the logon name <br/> cookie. values ["loginname"] = userinfo. username; <br/> // read logon time <br/> cookie. values ["da Tetime "] = datetime. now. tostring (); <br/> cookie. values ["count"] = count. tostring (); <br/> cookie. expires = datetime. now. adddays (30); <br/> // update the current cookie value in the cookie set <br/> response. cookies. set (cookie); </P> <p >}</P> <p> response. write (userinfo. username + "welcome"); <br/>}</P> <p> This. lblleavetime. TEXT = datetime. now. tostring (); <br/>}</P> <p> // obtain the cookie value of the customer named "login" <br/> If (request. cookies ["L Ogin "]! = NULL) <br/>{< br/> // multiple values <br/> string loginname = request. cookies ["login"] ["loginname"]. tostring (); <br/> If (loginname! = NULL) <br/>{< br/> response. write ("username" + loginname); </P> <p >}</P> <p> string time = request. cookies ["login"] ["datetime"]. tostring (); <br/> If (time! = NULL) <br/>{< br/> response. write ("Upload time" + time); <br/>}</P> <p> string COUNT = request. cookies ["login"] ["count"]. tostring (); <br/> If (count! = NULL) <br/>{< br/> response. write ("access times" + count); <br/>}</P> <p> protected void page_load (Object sender, eventargs E) <br/>{< br/> // declare a reference to an httpcookie <br/> httpcookie cookie = NULL; <br/> // count the number of visits <br/> int COUNT = 0; <br/> // determines whether the access request is a page addition. <br/> If (! Page. ispostback) <br/>{< br/> // read the "userinfo" in the session range, because we store the userinfo type, therefore, we can directly convert this type to <br/> userinfo = session ["userinfo"] As userinfo; <br/> If (userinfo = NULL) <br/> {<br/> response. redirect ("login. aspx "); <br/>}< br/> else if (userinfo. username! = "Jrain" | userinfo. Password! = "1234") <br/>{< br/> response. redirect ("login. aspx "); <br/>}< br/> else <br/> {<br/> // read the cookie named" login "from the client, <br/> cookie = request. cookies ["login"]; <br/> // If the cookie does not exist, the cookie is written for the first time. <br/> If (cookie = NULL) <br/>{< br/> // if the client does not have this cookie, We need to instantiate this name first.

Http://blog.163.com/pengpeng_614/blog/static/177071692008711102916339/

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.