asp.net action on cookies (foreground JS and background C # code)

Source: Internet
Author: User

CSDN ads are getting more and more, all blog notes are no longer updated, new URLs dotnet notes

A

The operation of the background C # code on cookies:

Take a look at the code

           -----Cookie creation \ Setup action----///////////////////////////////////////////                Guid gu_id = Guid.NewGuid (); Get a GUID to put in a cookie, do the test with HttpCookie cookie = new HttpCookie ("name1");//Create a cookie Date called name1                 Time dt = DateTime.Now; Gets the current time TimeSpan ts = new TimeSpan (0, 0, 0, 20, 0); The expiration time is 20 seconds cookie. Expires = dt.                ADD (TS); Set the expiration Time (Expires) to specify that her expiration time (dt+20 seconds) expires after the cookie. Value = gu_id.            ToString ();              Put the gu_id into the cookie Response.appendcookie (cookie);



            Adds an HTTP cookie to the (client) internal Cookie collection. -----Cookies Read the operation----/////////////////////////////////////////////* This code is also generally written separately on other pages
               Face, because the cookie is placed on the client, "this page write \ page read" The actual situation is not easy to use */if (request.cookies["name1"]!= null) { Response.Write (request.cookies["name1"). Value+ "<br/>");/Read NamE is the value of the cookie for "name1"////////////////////////////--------Cookie modification----////////////////////// if (request.cookies["name1"]!=null) {request.cookies["name1"]. Value = Guid.NewGuid (). ToString ();//sets a new value Response.Write (request.cookies["name1") for the cookie named "Name1".
            Value); }

Above code: two times read the same cookie with the values:

0de2084e-75f3-4427-9e6f-8f0b5c446847
c2f1afa0-c1e0-41aa-a29b-cb6519524e9e


Ways to delete cookies:

        <summary>
        ///Delete cookies
        ///</summary>
        ///<param name= "key" >cookie > Public
        void Clearcookies (string key) {
            HttpCookie cook = new HttpCookie (key);
            Cook. Expires = DateTime.Now.AddDays ( -1);
            Cook. Values.clear ();
            System.Web.HttpContext.Current.Response.Cookies.Set (cook);
        



Two

Take a look at the front desk JS (jquery) Action on cookies, introducing a cookie jquery plugin jquery.cookies.js

The operation of cookies is always with us when we visit a website, record our every move, and will not endanger the user's privacy information, will be saved, so that users do not have to go again to operate the repeated steps, so that greatly facilitate the customer, but also increase the customer's return to the site.

Jquery.cookie.js provides a very simple way to manipulate cookies in jquery. $.cookie (' The_cookie '); Get Cookie $.cookie (' The_cookie ', ' the_value '); Set Cookie $.cookie (' The_cookie ', ' The_value ', {expires:7}); Set a cookie with time, by default the $.cookie (' The_cookie ', ', ', {Expires:-1}) is calculated by day; Delete $.cookie (' The_cookie ', null); Delete Cookie $.cookie (' The_cookie ', ' The_value ', {expires:7, path: '/', Domain: ' jquery.com ', secure:true});/Create a new cookie Include expiration path domain name, etc.

Senior Experience



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.