Asp. NET the use of cookies

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">Ready to start writing backstage code, but just see the cookie is dumbfounded, online collected some information, summed up a bit, beginners can see.</span></p></p><p><p><span style="font-family: Microsoft YaHei; color: #888888; font-size: 14px;">Create cookies</span></p></p><pre class="brush"><pre class="brush"><span style="font-family: Microsoft YaHei; font-size: 14px;">System.Web.HttpCookie cookie=new HttpCookie ("uid");//create a cookiecookie named UID with a value of 20. Value= "a"; a Cookie. Expires=datetime.now.adddays (1); Response.appendcookie (user_cookies_id);<br></span></pre></pre><p><p><span style="background-color: #ffffff; font-family: Microsoft YaHei; color: #888888; font-size: 14px;">Assign a value to a cookie</span><br><span style="font-family: Microsoft YaHei; font-size: 14px;">Response.cookies["uid"]. Value = "20";</span><br><span style="font-family: Microsoft YaHei; font-size: 14px;">Response.cookies["logincode"]. Value = "58469";</span></p></p><p><p><span style="font-family: Microsoft YaHei; color: #888888; font-size: 14px;">Controlling the validity of cookies</span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">Response.cookies["uid"]. Expires = DateTime.Now.AddDays (1);</span><br><span style="font-family: Microsoft YaHei; font-size: 14px;">Response.cookies["logincode"]. Expires = DateTime.Now.AddDays (1);</span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">The above is the validity of the two variables for 1 days, of course, a few hours, the method is as Follows:</span><br><span style="font-family: Microsoft YaHei; font-size: 14px;">Response.cookies["logincode"]. Expires =datetime.now.addhours (1);</span><br><span style="font-family: Microsoft YaHei; font-size: 14px;">This is valid for 1 hours, can be analogous to a few minutes, a few seconds and so On.</span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;"><span style="color: #888888;">Delete Cookies</span></span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">The Remove method that invokes a collection of cookies removes the cookie from the Server-side collection so that the cookie is not sent to the Client. however, If a cookie is already present on the client, the cookie is assigned a past expiration date:</span></p></p> <ol> <ol> <ol> <li><p><span style="font-family: Microsoft YaHei; font-size: 14px;">Determines whether the cookie exists and creates a new cookie with the same name if it Exists.</span></p></li> <li><p><span style="font-family: Microsoft YaHei; font-size: 14px;">Set the expiration date of the Cookie to a time in the Past.</span></p></li> <li><p><span style="font-family: Microsoft YaHei; font-size: 14px;">Add cookies to the <strong></strong> cookie collection Object.</span></p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">The following code example shows how to set a past expiration date for a Cookie.</span></p></li> </ol> </ol> </ol><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">1.</span></p></p><pre class="brush:csharp;gutter:true;"><pre class="brush:csharp;gutter:true;"><span style="font-family: Microsoft YaHei; font-size: 14px;">Response.Cookies.Remove ("mycookie"); Response.Redirect ("....")//steering </span></pre></pre><p><p>2.</p></p><pre class="brush:csharp;gutter:true;"><pre class="brush:csharp;gutter:true;"><span style="font-family: Microsoft YaHei; font-size: 14px;">If (request.cookies["usersettings"]! = Null) { HttpCookie mycookie = new HttpCookie ("usersettings"); Mycookie.expires = DateTime.Now.AddDays ( -1d); RESPONSE.COOKIES.ADD (mycookie);}</span></pre></pre><p><p></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">How do I get the value of these cookies? (it is generally determined before the call that this cookie exists). Method:</span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">If (request.cookies["uid"]! = Null)</span><br><span style="font-family: Microsoft YaHei; font-size: 14px;">{</span><br><span style="font-family: Microsoft YaHei; font-size: 14px;">String ruid = request.cookies["uid"]. Value;</span><br><span style="font-family: Microsoft YaHei; font-size: 14px;">}</span></p></p><pre class="brush:csharp;gutter:true;"><pre class="brush:csharp;gutter:true;"></pre></pre><p><p></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">Store multiple messages in a cookie:</span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">For example, add more information under a cookie named ASPCN</span></p></p><pre class="brush:csharp;gutter:true;"><pre class="brush:csharp;gutter:true;"><span style="font-family: Microsoft YaHei; font-size: 14px;">HttpCookie cookie = new HttpCookie ("aspcn"); Cookies. Values.add ("webmaster", "irrigation Small fish"); Cookies. Values.add ("writer", "beige"); Cookies. Values.add ("linkcolor", "blue"); Response.appendcookie (cookie);</span></pre></pre><p><p></p></p><p><p>Asp. NET the use of cookies</p></p></span>

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.