Javascript and asp.net cookie operations

Source: Internet
Author: User

Both javascript cookies and asp.net cookies point to the same thing. Not much nonsense.

1. Use asp.net to write cookies in the background, and use javascript to read

Protected void Page_Load (object sender, EventArgs e) {HttpCookie cookie = new HttpCookie ("MyCook"); // The name DateTime dt = DateTime of the First-made and set Cookie. now; TimeSpan ts = new TimeSpan (0, 0, 1, 0, 0); // The expiration time is 1 minute cookie. expires = dt. add (ts); // set the expiration time // string tmp = System. web. httpUtility. urlEncode ("Chinese", System. text. encoding. UTF8); cookie. values. add ("userid", "123"); cookie. values. add ("userid2", "456"); Response. appendCookie (cookie );}

Reading javascript at the front end

    <script type="text/javascript">        var allcookies = document.cookie;        function test() {            //alert(decodeURIComponent(allcookies));            alert(allcookies);        }    </script>

The front-end can obtain data normally.

Mybook = userid = 123 & userid2 = 456

But note that when asp.net inputs Chinese, it should be converted into a UTF-8 code

Protected void Page_Load (object sender, EventArgs e) {HttpCookie cookie = new HttpCookie ("MyCook"); // The name DateTime dt = DateTime of the First-made and set Cookie. now; TimeSpan ts = new TimeSpan (0, 0, 1, 0, 0); // The expiration time is 1 minute cookie. expires = dt. add (ts); // set the expiration time string tmp = System. web. httpUtility. urlEncode ("Chinese", System. text. encoding. UTF8); cookie. values. add ("userid", tmp); cookie. values. add ("userid2", "Chinese"); Response. appendCookie (cookie );}

Garbled characters are obtained when javascript is directly read at the front end!

MyCook = userid = % e4 % b8 % ad % e6 % 96% 87 & userid2 = Juan Xiao

Therefore, when entering Chinese characters

 

System. Web. HttpUtility. UrlEncode ("Chinese", System. Text. Encoding. UTF8 );

 

When reading with javascript

decodeURIComponent(allcookies)

Let's look at the example (you can use decodeURIComponent to read Chinese correctly.

decodeURIComponent(document.cookie);

MyCook = userid = Chinese & userid2 = Juan Xiao

2. Use javascript to write cookies on the front end, And asp.net reads

Front-end code

 

document.cookie = "MyCook=userid=123&userid2=456"

 

Background code

If (Request. Cookies ["MyCook"]! = Null) {// Response. write ("the key value of Cookie is userid:" + Request. cookies ["MyCook"] ["userid"]); // The whole line // Response. write ("the key value of Cookie is userid2" + Request. cookies ["MyCook"] ["userid2"]); Response. write (Request. cookies ["MyCook"]. value); // output all values}

 

 

 

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.