Cross-origin cookie sharing

Source: Internet
Author: User

From: http://www.cnblogs.com/Ihaveadream/archive/2008/05/02/1178538.html

Note the following three attributes of a cookie:
1. Domain domain
2. Path
3. expires expiration time

For cross-origin operations, you need to set the domain attributes:
Response. Cookies ("mycookie"). Domain = "cnblogs.com"; (this refers to a wildcard domain name)
In this way, access is available under other second-level domain names, and ASP. NET pass the test.

Access under the virtual directory:
I did a test on ASP, but I did not try. net. If the path attribute is not specified, Cookies cannot be shared under different virtual directories.
Set response. Cookies ("mycookie"). Path = "/".

General statement:
Response. Cookies ("mycookie"). Domain = "cnblogs.com ";
Response. Cookies ("mycookie"). Path = "/"
Response. Cookies ("mycookie"). expires = now + 365;
Response. Cookies ("mycookie") ("test") = "test ";

. Net clear cookie

1 httpcookie cookie = system. Web. httpcontext. Current. Request. Cookies [cookiename];
2 If (cookie! = NULL)
3 {
4 cookie. Values. Clear ();
5 setusercookieexpiretime (cookiename,-1 );
6 cookie. Domain = _ domain;
7 system. Web. httpcontext. Current. response. Cookies. Set (cookie );
8}
9 public static void setusercookieexpiretime (string key, int days)
10 {
11 system. Web. httpcontext. Current. response. Cookies [Key]. Domain = _ domain;
12 system. Web. httpcontext. Current. response. Cookies [Key]. Path = _ cookiepath;
13 system. Web. httpcontext. Current. response. Cookies [Key]. expires = datetime. Now. adddays (days );
14}
15

. NET add/update cookies

1 public static void addusercookies (string key, string value, string cookiename, string domain)
2 {
3 httpcookie cookie = system. Web. httpcontext. Current. Request. Cookies [cookiename];
4 If (cookie = NULL)
5 {
6 cookie = new httpcookie (cookiename );
7 cookie. Domain = domain;
8 cookie. Path = _ cookiepath;
9
10 cookie. Values. Add (Key, value );
11 httpcontext. Current. response. appendcookie (cookie );
12}
13 else
14 {
15 if (system. Web. httpcontext. Current. Request. Cookies [cookiename]. Values [Key]! = NULL)
16 {
17 cookie. Values. Set (Key, value );
18}
19 else
20 {
21 cookie. Domain = domain;
22 cookie. Path = _ cookiepath;
23
24 cookie. Values. Add (Key, value );
25 httpcontext. Current. response. appendcookie (cookie );
26}
27}
28}
29

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.